20

The following code snippet is a part of the solution given here:

\def\IncludeOutput#1{
  \parskip=2mm
  \def\examplename{#1}
  \pdfximage{\Directory/\examplename.pdf}%
  \edef\lastpdfpage{\the\pdflastximagepages}
  \repeat\for{pag}\by{0} % We advance manually the counter in the loop body
  \until{\ifnum\pag>\lastpdfpage}
  \do{
    \noindent\hfill\fbox{%
    \includegraphics[width=0.45\textwidth,page=\number\pag]{\Directory/\examplename.pdf}}%
    \hfill%
    \advance\pag by 1
    \ifnum\pag>\lastpdfpage\hfill\par% If odd number of pages
    \else% If even number of pages, output the last one
       \fbox{%
         \includegraphics[width=.45\textwidth,page=\number\pag]{\Directory/\examplename.pdf}}%
       \hfill\hbox{}\par % Next pair of images
    \fi%
    \advance\pag by 1
  }
}

I want to use \loop...\repeat constructs in the code snippet above instead of the current looping constructs which are defined in \input{repeat}.

I am not familiar with \loop...\repeat, especially for nested loops and conditional statements such as checking whether the number is odd or even, checking whether a number is greater or equal to another number.

  • I think I should rollback to get useful answers. – kiss my armpit Jul 6 '12 at 6:39
  • I think your earlier MWE was much more M, though I suppose just as little W. This one has too much going on. – Ryan Reich Jul 6 '12 at 6:50
  • It would be nicer if you explained what you want to achieve. – egreg Jul 6 '12 at 6:52
  • @egreg: I want to make the answer given by JLDiaz still work if I enclose it with \IfFileExists. – kiss my armpit Jul 6 '12 at 6:57
  • 2
    @HiggsBoson I believe you can use the following workaround: \newif\ifFE \IfFileExists{myfile}{\FEtrue}{\FEfalse} \ifFE ... \else ... \fi – yo' Jul 6 '12 at 7:09
17

This doesn't work with Knuth's TeX, because it requires e-TeX's \unless; but pdftex has it.

\newcount\X
\X=10

\loop
\the\X\endgraf
\advance \X by -1
\unless\ifnum \X<0
\repeat

\bye

There are no predefined operators for combining conditionals.

Loops with \loop...\if...\repeat are really easy:

\loop
<codeA>
<conditional>
<codeB>
\repeat

Usually one has either <codeA> or <codeB>, but not both (it's allowed to have both, though). The main difference is that <codeA> is always executed at least once, because it comes before the conditional.

What's the conditional? Any predefined or built one; <codeA> and <codeB> can also contain complete conditionals.

The \repeat command is defined (in LaTeX) as \fi, so that a loop can be nested in another conditional. Thus

\newif\ifhiggs

\IfFileExists{abc.tex}{\higgstrue}{\higgsfalse}
\ifhiggs
  \loop
  <codeA>
  <conditional>
  <codeB>
  \repeat
\fi

is a good way to avoid complicated code in the argument. You can even use Eijkhouts repeat.tex provided you redefine \repeat:

\input{repeat}
\let\Repeat\repeat
\let\repeat\fi

and use \Repeat instead of \repeat for the loops. However, I cannot recommend using that loop construction.

8

X >= 0 is the same as X>-1:

\newcount\X
\X=10
\loop
\the\X\endgraf
\advance \X by -1
\ifnum \X>-1
\repeat
\bye

or with a dummy value

\newcount\X
\newcount\Y
\X=10
\Y=0
\loop
\the\X\endgraf
\advance \X by -1
\ifnum\X>0 \ifnum\X=0\Y=1\fi\else\Y=1\fi
\ifnum\Y=0
\repeat
\bye
3

Yo can also use the math library that is part of pgf:

enter image description here

References:

Code:

\documentclass{article}
\usepackage{pgf}

\newcommand{\TestValue}[2]{%
\pgfmathsetmacro{\var}{#1-#2}%
\pgfmathparse{ifthenelse(\var>=0,"#1~is greater or equal to #2","#1~is less than #2")}
\pgfmathresult%
}%

\begin{document}
\TestValue{11}{10}\par
\TestValue{10}{10}\par
\TestValue{9}{10}\par
\end{document}
3

Raw TeX does not have amenities such as compound Boolean operators. Instead, you must nest your conditionals:

\newcount\X
\X=10

\newif\ifGEzero
\loop
\the\X\endgraf
\advance \X by -1
% The spaces after the 0's are important
\ifnum\X>0 \GEzerotrue
\else
 \ifnum\X=0 \GEzerotrue
 \else\GEzerofalse
 \fi
\fi
\ifGEzero
\repeat

\bye

The reason I made a \newif was because the \loop syntax forbids compound conditionals! Instead, you execute the whole conditional in the loop body, storing the logical result in the \ifGEzero test, and then begin that conditional as the loop condition. (See my answer to What is \iffalse for? for a philosophical explanation of this.)

2

Solved! It is time to sleep.

\newcount\x
\newcount\M
\newcount\N


\M=7% number of pages.
\N=\M
\advance\N by 1
\x=1

\loop
    \ifnum\x<\N
    \noindent\null\hfill page:\the\x
    \advance \x by 1
    \ifnum\x>\M
        \hfill\null\endgraf
    \else
        \hfill page:\the\x\hfill\hbox{}\endgraf
    \fi
    \advance \x by 1
\repeat

\bye
  • My answer based on combining the existing answers. – kiss my armpit Jul 6 '12 at 7:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.