# Ignoring parts of text inside macro

Consider the following code:

\documentclass{book}
\usepackage{pgffor}

\begin{document}
\newcommand\containers{
\cont{aaa{\bf don't xstring me}aaa}
\cont{bbb\large bbb}
\cont{cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccI'm very long. don't \textbf{mbox} me. I'm going to be in very odd text widths etccccccccccccccccccccccc}
\cont{last one(\mbox{ddddd})}
}
\cont{yea here i am}
\cont{and me too}
}

\newcount\cntlines
\newcount\cntline
\newcommand\cont[1]{%
\ifnum\cntline=\cntlines%
#1%
\fi%
}
\newcommand\passover[2]{%
\cntlines=#1\relax\cntline=0\relax%
#1
#2%
}

\foreach\idx in {6, 5, ..., 1}{%
\idx\footnote{\passover{\idx}{\containers}}
}
\end{document}


As can be seen, the code produces the 6 lines as footnotes, in reverse order. I'm aware that there are nicer ways to produce this output. This is only an example of some (not too trivial) processing I need to do over the text lines I get as input.

However, that's nice when I really have the text contained in such containers. but my actual input text will is different. It contains separators, as can be seen in the following implementation:

\documentclass{book}
\usepackage{pgffor}

\begin{document}

\newcommand\separators{%
aaa{\bf don't xstring me}aaa
\sep{}bbb\large bbb
\sep{}cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccI'm very long. don't \textbf{mbox} me. I'm going to be in very odd text widths etccccccccccccccccccccccc
\sep{}last one(\mbox{ddddd})
\seperatorsevenmore{}
}
\sep{}yea here i am
\sep{}and me too
}
\newcommand\seperatorsevenmore{
\sep{}another one
}

\newcount\cntlines
\newcount\cntline
\newsavebox\voidbox
\newcount\inbox \inbox=0\relax
\newcommand\sep{%
\ifnum\cntline=\cntlines%
\end{lrbox}%
\fi%
\ifnum\cntline>\cntlines\ifnum\inbox=0\relax%
\begin{lrbox}{\voidbox}\inbox=1\relax%
\fi\fi%
}
\newcommand\passsep[2]{%
\cntlines=#1\relax\cntline=0\relax%
#1
\begin{lrbox}{\voidbox}\inbox=1\relax\sep{}#2\sep{}\end{lrbox}%
}

\foreach\idx in {6, 5, ..., 1}{%
\idx\footnote{\passsep{\idx}{\separators}}
}

\end{document}


As can be seen, the results are pretty much the same (leave aside some differences in the blanks - this will be fixed easily).

But the main disadvantage of the second example is that I'm not skipping the undesired lines, but rather inserting all of them into a box. This is not noticed when having 6 lines, but my actual code arrives at some thousands of lines, so the difference in times becomes very significant (in terms of minutes).

What I need in fact is a way to void/ignore/skip parts of the text, while still running some selected macros inside it. I believe I need some way to pre-expand the input macro, e.g. by substituting somehow, so eventually I'll get something that resembles the containers macro, and only then actually "run" it.

I tried this approach:

\documentclass{book}
\usepackage{pgffor}
\usepackage{environ}

\begin{document}
\newcommand\separators{%
aaa{\bf don't xstring me}aaa
\sep{}bbbbbb
\sep{}cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccI'm very long. don't mbox me. I'm going to be in very odd text widths etccccccccccccccccccccccc
\sep{}last one(ddddd)
}
\sep{}yea here i am
\sep{}and me too
}

\newcount\cntlines
\newcount\cntline
\NewEnviron{tocont}{%
\ifnum\cntline=\cntlines%
\BODY%
\fi%
}
\newcommand\sep{%
\end{tocont}\begin{tocont}%
}
\newcommand\passenv[2]{%
\cntlines=#1\relax\cntline=0\relax%
\begin{tocont}#2\end{tocont}%
}
\foreach\idx in {6, 5, ..., 1}{%
\idx\footnote{\passenv{\idx}{\separators}}
}
\end{document}


But it fails compilation, and anyway it's too simple to be true...

Can anybody suggest an idea?

EDIT: I've added some attributes to the lines in the examples above, to emphasize that they may contain any design related macro that is allowed in footnotes.

What I need is a way to expand only the \sep{} macro and macros starting with \seperators (as in my example: all macros that add to the list of text lines are called \seperators<something>).

From one \sep{} macro, I want to "skip" to the next one (either contained in the \seperators{} macro directly, or inside such sub-\seperators...). let's assume, if this simplifies, that I only have a single level of indirection. That is, a sub-\seperators... won't call another sub-\seperators..., but it may use other macros (\textbf, \mbox, and anything else you can imagine) and of course \sep{}.

The \sep{} macro decides whether to ignore all text until next one, or expand and output it. Just as if the text in between was enclosed in a \cont{} as above.

As shown, I can do it by dumping the contents into a dummy box. But is there a type of box that tells TeX "don't waste time on organizing me. just spill the contents and everything will be ok"? That should be equivalent to skipping it, I guess.

Alternatively, a way to take a macro and "create" another one based on it, some "functions" that use the raw macro code and just "substitute" "}\cont{" instead of every "\sep{}"? In this case, I will need 2 passes - one to expand all the "\seperators...{}" lines, the 2nd to perform the mentioned string manipulation. Then I just "run" the result as a macro to be expanded regularly.

ANOTHER EDIT: it turns out that I don't need the option to include additional \seperators... inside. that is, I only need to deal with the raw \sep{} commands directly in the macro.

Does that make the problem solvable?

• It would help if you specify what and under what conditions something has to be skipped. – egreg Nov 4 '14 at 8:02
• @egreg, Thanks for your comment. I've added a clarification at the bottom of the question. I hope it explains more sharply what I think I need... – Avner Shapiro Nov 4 '14 at 8:38
• also I was mistaken assuming that a "real dummy" box will be equivalent. since TeX MUST expand all macros on its way, just to make sure all side effects occur (e.g. advancing \cntline), this is probably what takes most of the time, even if "leaf" formatting operations are given up with. – Avner Shapiro Nov 4 '14 at 9:58