# Recover TeX source from automatically generated LaTeX files

Maybe the question can not be exactly understood from the title. So, to automate things a bit, instead of rewriting everything when adding different pictures and tables, code was made to add everything with a help of FOR cycles:

\usepackage{pgffor}

\def\tableinput#1#2#3#4{%
\foreach \indexc in {#1, ..., #2} {%
\subsection{C\indexc -look explanations}
\foreach \indexd in {#3, ..., #4} {%
\subsubsection{C\indexc D\indexd -look explanations}
\rotatebox{90}{
\input{table-July-c\indexc d\indexd.tex}
}
\includegraphics[scale=0.6]{plot-July-c\indexc d\indexd.eps}
}
}
}


Afterwards I get everything included and everything works smooth. What I want is to be able to get the generated tex file (instead of just the PDF/DVI result). I could write C/C#/whatewer program that generates me the text, but since I already have the working stuff, I was wondering is there any possibility to grab the generated text?

I would like to be able to have something like:

\subsection{C0 -look explanations}
\subsubsection{C0 D0 -look explanations}
\rotatebox{90}{
\input{table-July-c0d0.tex}
}
\includegraphics[scale=0.6]{plot-July-c0d0.eps}
\subsubsection{C0 D1 -look explanations}
\rotatebox{90}{
\input{table-July-c0d1.tex}
}
\includegraphics[scale=0.6]{plot-July-c0d1.eps}
\subsection{C1 -look explanations}
\subsubsection{C1 D0 -look explanations}
\rotatebox{90}{
\input{table-July-c1d0.tex}
}
\includegraphics[scale=0.6]{plot-July-c1d0.eps}
\subsubsection{C1 D1 -look explanations}
\rotatebox{90}{
\input{table-July-c1d1.tex}
}
\includegraphics[scale=0.6]{plot-July-c1d1.eps}


Is that possible and if yes, how?

-
The expanded tex code that you show can't be "recovered" in any real sense as it would never have been generated. TeX loops (of any variety) are almost always executed as they go so each iteration would be typeset before the macros will expand again and cause the commands for the next iteration to be placed on the token stream. the commands you show in your expansion such as \subsection \includegraphics will have been expanded repeatedly at each iteration of the loop. –  David Carlisle Oct 4 '12 at 16:47
If your goal is just debugging, you could have a look at the trace package. –  JanKanis Nov 7 '12 at 13:49
@Somejan - the aim is to have a hold on the generated text (like copy/paste it) –  Vairis Nov 9 '12 at 9:32

While the expanded code can not be recovered, your can generate the desired code. The MWE below creates a file foobar.tex with the following lines:

\subsection{C 0-look explanations}
\subsubsection{C0D0-look explanations}
\rotatebox{90}{\input{table-July-c0d0.tex}}
\includegraphics[scale=0.6]{plot-July-c0d0.eps}
\subsubsection{C0D1-look explanations}
\rotatebox{90}{\input{table-July-c0d1.tex}}
\includegraphics[scale=0.6]{plot-July-c0d1.eps}
\subsection{C 1-look explanations}
\subsubsection{C1D0-look explanations}
\rotatebox{90}{\input{table-July-c1d0.tex}}
\includegraphics[scale=0.6]{plot-July-c1d0.eps}
\subsubsection{C1D1-look explanations}
\rotatebox{90}{\input{table-July-c1d1.tex}}
\includegraphics[scale=0.6]{plot-July-c1d1.eps}


## Notes:

• I combined the following three lines into one:

 \subsubsection{C\indexc D\indexd -look explanations}
\rotatebox{90}{
\input{table-July-c\indexc d\indexd.tex}


Otherwise they need to be rewritten as:

 \immediate\write\foobar{\rotatebox{90} }% <-- missing curly brace
\immediate\write\foobar{\input{table-July-c\indexc d\indexd.tex}}
\immediate\write\foobar{\Z}% <-- replaced closing curly

• Much thanks to David Carlisle for his help in use of \string macros.

## Further Enhancements:

• A huge improvement to this would be to modify the \RedefineMacros macro so that each of the redefined macros does the \immediate\write\foobar, which would then leave the code in the foreach loop completely readable. I attempted to do this but ran into problems. Will see if I can do this enhancement later.

## Code:

\documentclass{article}

\usepackage{pgffor}
\usepackage{graphicx}

\edef\Y{\string{}
\edef\Z{\string}}

\newcommand*{\RedefineMacros}{%
\def\subsection{\string\subsection}%
\def\subsubsection{\space\space\string\subsubsection}%
\def\input{\string\input}
\def\includegraphics{\space\space\space\space\string\includegraphics}
\renewcommand{\rotatebox}[1]{\space\space\space\space\string\rotatebox\Y##1\Z}%
}%
\newwrite\foobar
\newcommand{\tableinput}[4]{%
\RedefineMacros
\immediate\openout\foobar=foobar.tex
\foreach \indexc in {#1, ..., #2} {%
\immediate\write\foobar{\subsection{C \indexc -look explanations}}
\foreach \indexd in {#3, ..., #4} {%
\immediate\write\foobar{\subsubsection{C\indexc D\indexd -look explanations}}
\immediate\write\foobar{\rotatebox{90}{\input{table-July-c\indexc d\indexd.tex}}}% <-- combined 3 lines
\immediate\write\foobar{\includegraphics[scale=0.6]{plot-July-c\indexc d\indexd.eps}}
}
}
\immediate\closeout\foobar
}

\begin{document}
\tableinput{0}{1}{0}{1}
\end{document}

-