# multido variable in \paragraph prematurely incremented by one

The compilation of the following MCE:

\documentclass{report}
\usepackage{fixltx2e}
\usepackage{multido}
\begin{document}
\multido{\i=1+1}{4}{%
\chapter{I am the chapter \i}
\section{I am a section of chapter \i}
\subsection{I am a subsection of chapter \i}
\subsubsection{I am a subsubsection of chapter \i}
% \paragraph{I am a paragraph of chapter \i}
% \subparagraph{I am a subparagraph of chapter \i}
}
\end{document}


1. the expected result,
2. a strange result if the line % \paragraph{...} is commented out because the variable \i is prematurely incremented by one and the last corresponding occurrence doesn't appear. If the line % \subparagraph{...} is additionally commented out, it is subject to this strange behavior, the \paragraph{...} working as expected.

In this thread of the French TeX forum : (in French, sorry), some contributors isolated the trouble that seems to be a real anomaly.

• unrelated to your problem, but I hit upon a surprising problem when trying out your code in a file called multido.tex... results on TL2013 in an error as multido.sty wants to load a file multido.tex from PSTricls but it is the test file which is loaded... – user4686 Sep 5 '13 at 20:43

The answer is probably "don't do that". \paragraph is (by default) an inline heading this means that it is held back until the next paragraph starts so that it can be typeset as part of that paragraph. But here the next paragraph does not start until the loop has spun and so \i has incremented.

If the paragraph is within the loop, it all works better:

\documentclass{report}
\usepackage{fixltx2e}
\usepackage{multido}
\begin{document}
\multido{\i=1+1}{4}{%
\chapter{Je suis le chapitre \i}
\section{Je suis une section du chapitre \i}
\subsection{Je suis une sous-section du chapitre \i}
\subsubsection{Je suis une sous-sous-section du chapitre \i}
\paragraph{Je suis un paragraphe du chapitre \i}
% \subparagraph{Je suis un sous-paragraphe du chapitre \i}
\mbox{}
}
\end{document}

• Why would \mbox{} not just be considered part of the paragraph text and have the loop spun again before setting it? – Werner Sep 5 '13 at 17:25
• @Werner it is the paragraph that's the point, it means the paragraph after \paragraph starts there. As it was, the paragraph after \paragraph was somewhere inside \chapter after \i incremented. – David Carlisle Sep 5 '13 at 17:54
• @DavidCarlisle In fact, I don't want to do that :) It is just when building a MCE for another purpose that I noticed this behavior. The point is that I thought that, apart the way they are numbered, \paragraph and \subparagraph behaved like \(sub(sub))section: I forgot the (in|out)line headings point. – Denis Bitouzé Sep 8 '13 at 8:17

(edit) 2017: since xint 1.1 (2014/10/28) one needs here \usepackage{xinttools}. Code updated for that to replace \usepackage{xint} of initial answer.

As an alternative one may use the expandable macro \xintApplyUnbraced from package xint. But we need first to generate expandably an arithmetic sequence, this not being provided in the package, I give one possible construct here.

The result of the code is that the enumeration is as expected, contrarily to the phenomenon with \multido.

\documentclass{report}

\usepackage{fixltx2e}
\usepackage{multido}
\usepackage{xinttools}

% we define an expandable macro \indices {a+b}{N} to generate
% {a}{a+b}{a+2b}...{a+(N-1)b}
\catcode\_ 11
\def\indices    #1#2{\indices_A #1.#2.}
\def\indices_A  #1+#2.#3.{\indices_B {#3}{#1}{#2}{}}
\def\indices_B  #1#2#3{\ifnum #1>0
\expandafter\expandafter\expandafter
\indices_C
\else
\expandafter\indices_end
\fi \expandafter
{\the\numexpr #1-1\expandafter}\expandafter
{\the\numexpr #2+#3}{#3}{#2}}
\def\indices_end #1#2#3#4#5#6#7{#7}
\def\indices_C #1#2#3#4#5{\indices_B {#1}{#2}{#3}{#5{#4}}}
\catcode\_ 8

\begin{document}

\multido{\i=1+1}{4}{%
\chapter{Je suis le chapitre \i}
\section{Je suis une section du chapitre \i}
\subsection{Je suis une sous-section du chapitre \i}
\subsubsection{Je suis une sous-sous-section du chapitre \i}
\paragraph{Je suis un paragraphe du chapitre \i}
% \subparagraph{Je suis un sous-paragraphe du chapitre \i}
}

\def\stuff #1{{}% this {} is to stop the expansion
\chapter{Je suis le chapitre #1}
\section{Je suis une section du chapitre #1}
\subsection{Je suis une sous-section du chapitre #1}
\subsubsection{Je suis une sous-sous-section du chapitre #1}
\paragraph{Je suis un paragraphe du chapitre #1}
% \subparagraph{Je suis un sous-paragraphe du chapitre #1}
}

\xintApplyUnbraced\stuff{\indices{5+1}{4}}
% generates for 5,6,7,8.

\end{document}

• As already said, I don't want to do sort of things :) But interesting code! – Denis Bitouzé Sep 8 '13 at 9:35