生成一个包含整个文档的所有条目的表格

生成一个包含整个文档的所有条目的表格

我们正在编写一份技术文档,其中我们在整个文档的不同章节/部分中定义消息。所有消息都具有相同的结构并显示在表格中。我们认为在文档末尾列出所有这些消息的列表会很好。

因此我开始研究一个解决方案,将所有表格条目存储在单独的文件中,该文件将包含在文档的末尾。

这是我的工作解决方案:

\documentclass{article}
\RequirePackage{fontspec}
\usepackage{longtable, colortbl,environ}

\NewEnviron{tbl}
{%
\begin{longtable}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}%
\hline\rowcolor[gray]{.8}%
Column 1 & Column 2 & Column 3 & Column 4\\\hline%
\endhead%
\BODY%
\end{longtable}%
}

\newwrite\mytabularwrite
\newcommand{\writetotablefile}[1]{\immediate\write\mytabularwrite{\unexpanded{#1}}}

\newcommand{\myrow}[4]{\writetotablefile{\unexpanded{#1&#2&#3&#4\\\hline}}%
#1&#2&#3&#4\\\hline%
}

\AtBeginDocument{\immediate\openout\mytabularwrite=\jobname-table.tex}
\AtEndDocument{}

\begin{document}

\section{Table 1}
\begin{tbl}
\myrow{1äöü€}{2}{3}{4}
\end{tbl}

\section{Table 2}
\begin{tbl}
\myrow{5}{6}{7}{8}
\end{tbl}

\section{Complete Table}

\begin{tbl}
% ATTENTION: Starting with that point here: don't use \myrow anymore! Temp. file is already closed.
\immediate\closeout\mytabularwrite
\input{\jobname-table}
\end{tbl}

\end{document}

我必须解决的问题是 (La)TeX 无法以追加模式将文本写入文件。我找到了一些解决方法,例如:读取临时文件、添加新数据、存储临时文件、重复……但我认为这并不高效。

我现在从文档的开头打开临时文件,然后在将临时文件包含到最终表之前将其关闭。

好的,我有一个可行的解决方案,问题在哪里?目前没有问题。但我想问一下,是否有更好的方法解决这个问题。

再见,谢谢,aronadal

答案1

使用ToC类似方法的优点在于,该文件可以在文档的任何时间被包含,但只能包含一次。

ToC与往常一样longtable:编译多次!

\documentclass{article}
\RequirePackage{fontspec}


\usepackage{tocloft}
\usepackage{longtable, colortbl,environ}



\makeatletter


\def\@starttof#1#2{%
  \begingroup
  \makeatletter
  \renewcommand{\arraystretch}{1.5}
  % Table format may be changed%%%
  \begin{longtable}{|*{4}{p{2cm}|}}
    \hline\rowcolor[gray]{.8}%
    Column 1 & Column 2 & Column 3 & Column 4\\\hline%
    \endhead%

    \@input{\jobname.#1}%
  \end{longtable}
  \if@filesw
  \expandafter\newwrite\csname tf@#1\endcsname
  \immediate\openout \csname tf@#1\endcsname \jobname.#1\relax
  \fi
  \endgroup
}



\newcommand{\listofstuff}{%
  \@starttof{stuff}{}%
}



\NewEnviron{tbl}
{%
\begin{longtable}{|*{4}{p{2cm}|}}
  \hline\rowcolor[gray]{.8}%
  Column 1 & Column 2 & Column 3 & Column 4\\\hline%
  \endhead%
  \BODY%
\end{longtable}%
}

\DeclareRobustCommand{\tablerow}[4]{%
  #1&#2&#3&#4 \\ 
  \hline
}


\newcommand{\myrow}[4]{%
  \addtocontents{stuff}{\tablerow{#1}{#2}{#3}{#4}}
  #1&#2&#3&#4\\\hline%
}


\begin{document}
\section{Table 1}
\begin{tbl}
\myrow{1äöü€}{2}{3}{4}
\end{tbl}

\section{Table 2}
\begin{tbl}
\myrow{5}{6}{7}{8}
\end{tbl}

\section{Complete Table}

\listofstuff

\end{document}

改良版

\documentclass{article}
\RequirePackage{fontspec}


\usepackage{xparse}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{environ}


\newcommand{\foo}{Some stuff}
\newcommand{\foobar}{Other stuff}

\makeatletter


\NewDocumentCommand{\@starttof}{smO{}}{%
  \begingroup
  \makeatletter
  \renewcommand{\arraystretch}{1.5}
  \IfBooleanTF{#1}{%
    \section*{#3}%
  }{%
    \section{#3}%
  }%
%  Table format may be changed%%%
  \begin{longtable}{|*{4}{p{2cm}|}}
    \hline\rowcolor[gray]{.8}%
    Column 1 & Column 2 & Column 3 & Column 4\\
    \hline%
    \endhead%
    \@input{\jobname.#2}%
  \end{longtable}
  \if@filesw
  \expandafter\newwrite\csname tf@#2\endcsname
  \immediate\openout \csname tf@#2\endcsname \jobname.#2\relax
  \fi
  \endgroup
}



\NewDocumentCommand{\listofstuff}{sO{Complete Table}}{%
  \IfBooleanTF{#1}{%
    \@starttof*{stuff}[#2]%
  }{%
    \@starttof{stuff}[#2]%
  }%
}



\NewEnviron{tbl}
{%
  \begin{longtable}{|*{4}{p{2cm}|}}
    \hline\rowcolor[gray]{.8}%
    Column 1 & Column 2 & Column 3 & Column 4\\\hline%
  \endhead%
  \BODY%
\end{longtable}%
}

\DeclareRobustCommand{\tablerow}[4]{%
  #1&#2&#3&#4 \\ 
  \hline
}


\newcommand{\myrow}[4]{%
  \addtocontents{stuff}{\tablerow{#1}{#2}{#3}{#4}}
  #1&#2&#3&#4\\
  \hline%
}


\begin{document}


\listofstuff*

\section{Table 1}
\begin{tbl}
\myrow{1äöü€}{2}{3}{4}
\end{tbl}



\section{Table 2}
\begin{tbl}
\myrow{5}{6}{7}{8}
\end{tbl}

\begin{tbl}
\myrow{\foo}{6}{\foobar}{8}
\end{tbl}



\end{document}

在此处输入图片描述

使用页码更新

\documentclass{article}
\RequirePackage{fontspec}


\usepackage{xparse}
\usepackage{longtable}
\usepackage{colortbl}
\usepackage{environ}


\newcommand{\foo}{Some stuff}
\newcommand{\foobar}{Other stuff}

\makeatletter


\NewDocumentCommand{\@starttof}{smO{}}{%
  \begingroup
  \makeatletter
  \renewcommand{\arraystretch}{1.5}
  \IfBooleanTF{#1}{%
    \section*{#3}%
  }{%
    \section{#3}%
  }%
%  Table format may be changed%%%
  \begin{longtable}{|*{5}{p{2cm}|}}
    \hline\rowcolor[gray]{.8}%
    Column 1 & Column 2 & Column 3 & Column 4 & Page\\
    \hline%
    \endhead%
    \@input{\jobname.#2}%
  \end{longtable}
  \if@filesw
  \expandafter\newwrite\csname tf@#2\endcsname
  \immediate\openout \csname tf@#2\endcsname \jobname.#2\relax
  \fi
  \endgroup
}



\NewDocumentCommand{\listofstuff}{sO{Complete Table}}{%
  \IfBooleanTF{#1}{%
    \@starttof*{stuff}[#2]%
  }{%
    \@starttof{stuff}[#2]%
  }%
}



\NewEnviron{tbl}
{%
  \begin{longtable}{|*{4}{p{2cm}|}}
    \hline\rowcolor[gray]{.8}%
    Column 1 & Column 2 & Column 3 & Column 4\\\hline%
  \endhead%
  \BODY%
\end{longtable}%
}

\DeclareRobustCommand{\tablerow}[5]{%
  #1&#2&#3&#4&#5\\
  \hline
}

\newcommand{\myrow}[4]{%
  \addtocontents{stuff}{\tablerow{#1}{#2}{#3}{#4}{\thepage}}
  #1&#2&#3&#4\\
  \hline%
}


\begin{document}


\listofstuff*

\clearpage

\section{Table 1}
\begin{tbl}
\myrow{1äöü€}{2}{3}{4}
\end{tbl}


\clearpage

\section{Table 2}
\begin{tbl}
\myrow{5}{6}{7}{8}
\end{tbl}

\begin{tbl}
\myrow{\foo}{6}{\foobar}{8}
\end{tbl}



\end{document}

在此处输入图片描述

相关内容