对于我正在编写的文档,我希望实现以下目标:在文档的开头,对文档中提到的最重要的部分进行总结。我希望以一种相当自动化的方式实现这一点。这里给出了一个最小的工作示例,以便更好地形象化我想要实现的目标:
\documentclass{article}
\usepackage{datatool}
\usepackage{lipsum}
\DTLnewdb{mwe}
\newcommand\summary[1]{
\textbf{#1}
\DTLnewrow{mwe}
\dtlexpandnewvalue
\DTLnewdbentry{mwe}{entry}{#1}
\dtlnoexpandnewvalue
}
\newcommand\printsum{
\begin{itemize}
\item filler (preventing error)
\DTLforeach*{mwe}{\theEntry=entry}{
\item \theEntry
}
\end{itemize}
}
\begin{document}
\section{Summary}
\printsum
\section{Long text}
\lipsum[1]
\summary{First summary}
\lipsum[2]
\summary{Second summary}
\lipsum[3]
\summary{Third summary}
\section{Wanted effect (but then in the beginning)}
\printsum
\end{document}
我尝试寻找一些钩子,这些钩子仅在文档编译一次后显示一段文本,但找不到任何可以实现我想要的东西。除此之外,我不太确定如何实现我的目标,或者是否有可能实现。
有人能告诉我这个问题吗?任何有关这个问题的信息都非常感谢。提前谢谢。
答案1
您可以将收集到的摘要保存在辅助文件中\input
。我还实现了基于 MD5 的检查,以查看文件是否与开始时找到的文件不同,并在需要重新运行时发出警告。
\documentclass{article}
\usepackage{lipsum}
\ExplSyntaxOn
% the main command for printing and storing the summary
\NewDocumentCommand{\summary}{m}
{
\gdeman_summary:n { #1 }
}
% print the summaries inside itemize
\NewDocumentCommand{\printsum}{}
{
\begin{itemize}
\file_if_exist_input:nF { \c_sys_jobname_str.sum } { \item Nothing, so far, rerun LaTeX }
\end{itemize}
}
% set up so the summaries are written at end document
\AtEndDocument{ \__gdeman_summary_write: }
% check the file at begin document
\AtBeginDocument
{
\file_get_mdfive_hash:nN { \c_sys_jobname_str.sum } \l__gdeman_summary_hash_begin_tl
}
% variables
\seq_new:N \g_gdeman_summary_items_seq
\iow_new:N \g_gdeman_summary_write_iow
\tl_new:N \l__gdeman_summary_hash_begin_tl
\tl_new:N \l__gdeman_summary_hash_end_tl
% main function
\cs_new_protected:Nn \gdeman_summary:n
{
% print the summary
\textbf{#1}
% store it
\seq_gput_right:Nn \g_gdeman_summary_items_seq { #1 }
}
% write the summaries in an auxiliary file
\cs_new_protected:Nn \__gdeman_summary_write:
{
\iow_open:Nn \g_gdeman_summary_write_iow { \c_sys_jobname_str.sum }
\seq_map_inline:Nn \g_gdeman_summary_items_seq
{
\iow_now:Nn \g_gdeman_summary_write_iow { \item ##1 }
}
\iow_close:N \g_gdeman_summary_write_iow
\file_get_mdfive_hash:nN { \c_sys_jobname_str.sum } \l__gdeman_summary_hash_end_tl
\tl_if_eq:NNF \l__gdeman_summary_hash_begin_tl \l__gdeman_summary_hash_end_tl
{
\typeout{Summaries~differ,~rerun~LaTeX}
}
}
\ExplSyntaxOff
\begin{document}
\section{Summary}
\printsum
\section{Long text}
\lipsum[1]
\summary{First summary}
\lipsum[2]
\summary{Second summary}
\lipsum[3]
\summary{Third summary}
\end{document}