从自动生成的 LaTeX 文件中恢复 TeX 源

从自动生成的 LaTeX 文件中恢复 TeX 源

也许从标题无法准确理解这个问题。因此,为了使事情自动化一点,在添加不同的图片和表格时,无需重写所有内容,而是使用 FOR 循环编写代码来添加所有内容:

\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}
     }
   }
}

之后,我得到了所有东西,一切都运行顺利。我想要的是能够获得生成的 tex 文件(而不仅仅是 PDF/DVI 结果)。我可以编写 C/C#/whatewer 程序来生成文本,但由于我已经有了可以工作的东西,我想知道是否有可能获取生成的文本?

我希望能够拥有类似的东西:

\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}

这可能吗?如果可能,怎么做?

答案1

虽然扩展代码无法恢复,但您的生成所需的代码。下面的 MWE 创建一个foobar.tex包含以下行的文件:

\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}

笔记:

  • 我将以下三行合并为一行:

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

    否则需要将其重写为:

     \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
    
  • 非常感谢 David Carlisle 在使用\string宏方面提供的帮助。

进一步增强:

  • 对此的一个巨大改进是修改宏\RedefineMacros,以便每个重新定义的宏都执行\immediate\write\foobar,这将使foreach循环中的代码完全可读。我尝试这样做,但遇到了问题。稍后我会看看我是否可以做这个增强。

代码:

\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}

相关内容