I'm curious, when reading about the ifthen-packages I've come across the syntax of while-do statements. What are they used for ?

For example this piece of code from the documentation (this is copy-pasted):

\State $sum\gets 0$
\State $i\gets 1$
\While{$i\le n$}
\State $sum\gets sum+i$
\State $i\gets i+1$

I know it loops until a specific condition is met. But what use does this have in a LaTeX-Document, that isn't running in real time like a program. I mean it compiles but there aren't any variables that change during compilation are there?

  • Are you aware of the pgffor package? – marmot Jul 20 '18 at 15:16
  • No I am not. I'm very new to LaTeX. Should I be looking into it ? – Baumrinde Jul 20 '18 at 15:21
  • Well, it is up to you, but I believe the \foreach command comes rather close to what you want. It is documented on p. 901 ff of the pgfmanual. I personally learn most from examples. Perhaps you get a better answer if you detail an example where you want to use the while do stuff. – marmot Jul 20 '18 at 15:24
  • 1
    There seems to be a bit of confusion. LaTeX is used for typesetting, i.e., displaying content in a document. This content can be text, tables, images, graphs, etc, and also algorithms. For displaying an algorithm (i.e., a piece of text with programming statements in it) you can use the algorithmicx package, among other things. This does not in any way execute this code, it just displays the statements in a nicely formatted way. Now, apart from displaying code using LaTeX, you can also write code in LaTeX, and execute that, in order to automate the typesetting task to some extend. [...] – Marijn Jul 20 '18 at 15:54
  • 1
    Yes, exactly, a possible use case is when you are writing a scientific paper about a particular algorithm and you want to display the pseudo-code of the algorithm to illustrate and clarify the text. – Marijn Jul 20 '18 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.