GeneralDefining and manipulating variables in LaTeX

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
Bjarke
Posts: 4
Joined: Mon Jul 23, 2012 12:15 am

Defining and manipulating variables in LaTeX

Post by Bjarke »

Hi there

Does anybody know of a way to declare and manipulate variables in LaTeX in a way similar to a 'real' programming language?
Concretely, let's say I want to define a command \saybla{n} that prints 'bla' n times. What I'd like to write in my preamble is (in pseudocode) something like

Code: Select all

\newcommand{\saybla}[1]{
  if(#1 == 1)bla
  else bla \saybla{#1 - 1}
}
Or without recursion:

Code: Select all

\newcommand{\saybla}[1]{
  for(int i = 0; i < #1; i++){
    bla 
  }
}
Can somebody tell me if this is even possible in LateX and if so, how it's done?

Thanks.
Last edited by Stefan Kottwitz on Mon Jul 23, 2012 12:41 am, edited 1 time in total.

Recommended reading 2024:

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

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

Stefan Kottwitz
Site Admin
Posts: 10347
Joined: Mon Mar 10, 2008 9:44 pm

Defining and manipulating variables in LaTeX

Post by Stefan Kottwitz »

Hi Bjarke,

welcome to the board!

It seems that your question is not about defining and manipulating variables, but about conditional expressions and loops.

LaTeX can do this. For features which don't seem easy with base LaTeX, there are often packages, and that's also the case here.
  • For conditional expressions, have a look at the ifthen package, which provides a syntax for if ... then ... else. Pure TeX also provides basic "if" commands. Besides that, etex and etoolbox offer similar syntax, but more efficient.
  • Regarding forloops, you could use the forloop package. TikZ and pgf provide an extended and powerful syntax for such loops.
Stefan
LaTeX.org admin
Bjarke
Posts: 4
Joined: Mon Jul 23, 2012 12:15 am

Re: Defining and manipulating variables in LaTeX

Post by Bjarke »

Thanks for the quick reply. I'm familiar with both loops and conditional statements - I just wrote my problem in pseudocode for readability - perhabs a bit unclear.
I tried defining a counter to control a for-loop, but LaTeX doesn't support local variables, it seems, so I got an error the second time I called the command because the counter is already defined.
This can probably be solved by defining the counter outside of the \newcommand, but I'd really like to do the recursive solution as it is much more elegant. For that, I need to be able to define an integer (say 5), and subtract one from it, so I can do this:
\newcommand....
n = #1
\saybla{n-1}

Is that possible?
Thanks.
Bjarke
Posts: 4
Joined: Mon Jul 23, 2012 12:15 am

Re: Defining and manipulating variables in LaTeX

Post by Bjarke »

Thanks for the quick reply. I think my question was misunderstood - I'm familiar with both the ifthen package and loops. What I can't work out is how to define an integer and modify it (substracting one in this case). I'd be happy if I for instance could do this:

\newcommand{\oneless}[1]{
#1 - 1
}

Such that \oneless{5} gives 4 in the compiled document. Do you know if that's possible?
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Defining and manipulating variables in LaTeX

Post by cgnieder »

The easiest way would be to use \numexpr or etoolbox's or expl3's functions (all of which use e-TeX):

Code: Select all

\documentclass{article}

\newcommand*\saybla[1]{%
  \ifnum#1<0 \ERROR\fi
  \ifnum#1=1
    bla%
  \else
    bla \saybla{\numexpr#1-1}%
  \fi
}

% or with etoolbox commands:
\usepackage{etoolbox}
\newrobustcmd*\sayblah[1]{%
  \ifnumless{#1}{0}{\ERROR}{}%
  \ifnumequal{#1}{1}
    {blah}
    {blah \sayblah{#1-1}}%
}

% or with expl3:
\usepackage{expl3}
\ExplSyntaxOn
\newcommand*\sayblub[1]
  {
    \int_compare:nT  { #1 < 0 } { \ERROR }
    \int_compare:nTF { #1 = 1 }
      { blub }
      { blub~\sayblub{#1 - 1} }
  }
\ExplSyntaxOff

\begin{document}

\saybla{5} \sayblah{5} \sayblub{5}

\end{document}
Regards
site moderator & package author
Bjarke
Posts: 4
Joined: Mon Jul 23, 2012 12:15 am

Defining and manipulating variables in LaTeX

Post by Bjarke »

cgnieder wrote:

Code: Select all

\newcommand*\saybla[1]{%
  \ifnum#1<0 \ERROR\fi
  \ifnum#1=1
    bla%
  \else
    bla \saybla{\numexpr#1-1}%
  \fi
}
That's exactly what I was looking for - thanks alot!
Post Reply