现在应该写哪些正确的仪式?

现在应该写哪些正确的仪式?

(对标题表示歉意)

我刚刚开始尝试让一个 LaTeX 文件在进行过程中写出一些有用的东西。简而言之,我希望我的讲座 LaTeX 文件能够写出一个文件,在执行 LaTeX 后,该文件将为我提供相应的课程计划。因此,在(主)文档的开头,我想写出页眉,然后随着文档的进展,我想写出计划中的部分内容,然后在最后需要写出页脚。

令我困惑的是什么时候写入发生。讲座文件包含很多讲座(它是一个 beamer 文件),并且只有一个讲座在 LaTeX 中生成,但我想写出的信息之一是讲座标题,而我总是得到最后一个讲座标题。我认为这可能是\immediate答案,但这确实意味着即时。我想要的是将 放入\write宏中,这样当调用宏时,写入是立即的(特别是,我想将其挂接到讲座标题页中)。所以问题是:我该怎么做?

我可能只是忽略了一些显而易见的东西,但无论它是什么,我都忽略了它!

(并且由于某种原因,在谷歌上搜索“latex write”并没有多大帮助。)

答案1

我想我可能没理解这个问题。以下说法有什么问题:

\newcommand{\mywrite}[1]{\immediate\write\myoutputfile{#1}}

尽管我希望更完整一些,并处理并非所有输入文件都可能在每次编译时都被读取的事实(由于\includeonly),但您仍然希望获得完整的课程计划,即使某些部分存在于非包含文件中,用户也可能希望通过关闭所有文件输出\nofiles,或者只想停止自动更新课程计划,以便他们可以进行一些手动编辑(此处删除该\makemyrecords行)。我还想避免在文档中放置虚假空格,因此我会这样做,大致模仿\makeindex乳胶中的工作方式:

\ProvidesPackage{myrecords}%
          [2010/08/14 v0.01 my example record package (LSB)]
\NeedsTeXFormat{LaTeX2e}

\def\myrec@outputfileextension{.mrc}
\newcommand*{\myrec@write}[1]{}

\def\myrec@verb{\expandafter\strip@prefix\meaning}

\newcommand*{\makemyrecords}{%
    \newwrite\myrec@outfile
    \immediate\openout\myrec@outfile=\jobname\myrec@outputfileextension
    \typeout{Writing my records to file \jobname\myrec@outputfileextension}%
    \let\makemyrecords\@empty
    \renewcommand*{\myrec@write}[1]{\immediate\write\myrec@outfile{##1}}%
}
\@onlypreamble\makemyrecords

\newcommand*{\myrecord}[1]{%
    \@bsphack
    \if@filesw
        \immediate\write\@auxout{\string\myrecordentry{#1}}%
    \fi
    \@esphack
}

\newcommand*{\myliteral}[1]{%
    \@bsphack
    \if@filesw
        \def\@tempa{#1}
        \immediate\write\@auxout{%
            \string\myrecordentry{%
                \myrec@verb\@tempa}}%
    \fi
    \@esphack
}

\newcommand*{\mywritecurrenttitle}{
    \if@filesw
        \immediate\write\@auxout{\string\myrecordentry{%
            \string\lessonplanitem{\currenttitle}}}%
    \fi
}

\newcommand*{\myrecordentry}[1]{}
\AtEndDocument{%
    \renewcommand*{\myrecordentry}[1]{%
        \def\@tempa{#1}%
        \myrec@write{\myrec@verb\@tempa}%
}}

\endinput

如果我根据您所想的内容来测试这一点(显然您希望将 \renewcommand{\currenttitle}\mywritecurrenttitle 与生成文档分段的命令结合起来,或者将它们放在您的文档类提供的挂钩中)。

\documentclass{article}
\usepackage{myrecords}
\makemyrecords
\begin{document}
\myliteral{
    \documentclass{lessonplan}
    \begin{document}}
\newcommand*{\currenttitle}{This is the title of lesson 1}
\mywritecurrenttitle
Lesson 1....

\renewcommand{\currenttitle}{This is the title of lesson 2}
\mywritecurrenttitle
Lesson 2.....

\myliteral{\end{document}}
\end{document}

然后,正如预期的那样,课程计划文件如下所示

\documentclass {lessonplan} \begin {document}
\lessonplanitem {This is the title of lesson 1}
\lessonplanitem {This is the title of lesson 2}
\end {document}

答案2

一种解决方案是使用filecontents(CTAN)。

答案3

您不能只使用\let来保存当前标题,然后稍后再使用它来写入文件吗?另一种选择是始终使用,\immediate以便尊重排版的时间顺序。

相关内容