GeneralLatex as a programming language

LaTeX specific issues not fitting into one of the other forums of this category.
lalop
Posts: 63
Joined: Fri Sep 11, 2009 11:25 pm

Latex as a programming language

Post by lalop »

Hi, I wish to use either switch-case statements, and/or programming arrays (not the matrix stuff, but actual arrays of variables for storing and accessing data like a[5][2]).

Essentially, I'm trying to make a command like:

\name[last]{person}

which would output the person's last/first name which I define after the fact, and has to work for different people. A switch case statement:

Switch{name}
{
Case name1:
Case name2:
}

Would be very helpful. Array access might be less helpful (unless it has auto-hashing), but I could always define a simple hash algorithm, and I'd have some other uses for arrays anyway. (For example, spamming short answers to problems with ANS[counter].)

Right now, I implement \name by numerous annoying \ifthenelse statements (because, after all, that's the same thing as a switch), but it's very annoying to read even. I appreciate any hints.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Latex as a programming language

Post by gmedina »

Well, can you please explain (to me, at least) what's your intention (from a typographical standpoint, of course) in requiring a typographical system to do such things?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
lalop
Posts: 63
Joined: Fri Sep 11, 2009 11:25 pm

Re: Latex as a programming language

Post by lalop »

What is this? I simply am not good at making up characters' names on the spot, and wish to insert code which allows me to name them later. As for quickly typing in short answers to questions, it's just more convenient than to type out

Score: 78/90
Score: 66/100
...

Everytime. I'd like to be able to stick something like [78,90 ; 66, 100 , ... ] in an array and just do
\score
\score
...
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Re: Latex as a programming language

Post by gmedina »

Ah, now I see your intent. Right now (I must confess), I'm really, really exhausted but I'm sure that someone else will offer helpful advise.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: Latex as a programming language

Post by josephwright »

I'd look in the first instance at the datatool package.
Joseph Wright
lalop
Posts: 63
Joined: Fri Sep 11, 2009 11:25 pm

Re: Latex as a programming language

Post by lalop »

Very nice find! This would seem to allow for arrays, of a sort.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Latex as a programming language

Post by gmedina »

Another option (for array data structures) could be the arrayjob package.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
lalop
Posts: 63
Joined: Fri Sep 11, 2009 11:25 pm

Re: Latex as a programming language

Post by lalop »

An even better find! (It's not nearly as roundabout.)

Still missing the switch *hint hint* <3
lalop
Posts: 63
Joined: Fri Sep 11, 2009 11:25 pm

Latex as a programming language

Post by lalop »

gmedina wrote:Another option (for array data structures) could be the arrayjob package.
Hi again,

arrayjob is almost passable. The only problem is that \checkarrayname seems to create a tab every time (and it's necessary, as it's the only way to actually access the array data to compare and stuff aside from actually printing it out).

For example, if an array is called Names, then

a\checkNames(2,3)b

gives out something like:

Code: Select all

a    b
Effectively, it puts a tab in the output. Is there any way to suppress the tab? Thanks.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Latex as a programming language

Post by gmedina »

I don't undestand your question. The followng simple example doesn't behave as you mention:

Code: Select all

\documentclass{article}
\usepackage{arrayjob}

\begin{document}

\newarray\Names
\readarray{Names}{Abel&Borel&Cantor&Dedekind&Eisenstein&Fermat&Galois&Hilbert}
\dataheight=4

a\checkNames(2,3)b

\cachedata

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply