以下部分是对之前问题的后续回答,以及 Enrico 的回答,如 使用文本中的引用自动从数据工具创建表格。
那里概述的方法有效,但速度相当慢。我想到提高速度的一种方法可能是将行标签信息明确写入单独的文件中。请考虑下面的示例。
线路
\PrintDocTable{dDB}{Documents}
打印表的所有行。但是,我可以选择明确列出要打印的行,方法是
\PrintDocTable[2014.11.14,2013.10.05.powai]{dDB}{Documents}
只要列出的行与引用的行相同,就没有问题。但是,保持这些与引用同步是可行的。
我认为应该可以让\ref
宏自动将标签列表写入与辅助文件不同的文件中,例如example.lab
,然后将该文件直接包含在参数中[]
。\PrintDocTable
就像这样:
\PrintDocTable[\include{example.lab}]{dDB}{Documents}
在这种情况下,example.lab
将包含文本“2014.11.14,2013.10.05.powai”。这可行吗?如果可行,我该如何修改宏\ref
?
###########################################
example.tex
###########################################
\documentclass{letter}
\usepackage[verbose]{datatool}
\usepackage{longtable}
\usepackage{array}
\usepackage{url}
\newcounter{tabenum}\setcounter{tabenum}{0}
\newcommand{\colhead}[1]{\multicolumn{1}{>{\bfseries}l}{#1}}
\newcommand{\nextnuml}[1]{\refstepcounter{tabenum}\thetabenum.\label{#1}}
\makeatletter
\let\oldref\ref
\def\ref#1{%
\immediate\write\@auxout{%
\string\gappto\string\ReferencedIDs{#1,}%
}%
\oldref{#1}%
}
\def\ReferencedIDs{}
\makeatother
\usepackage{fouriernc}
\address{Some Address\\ Some Place\\Email: [email protected]}
\signature{(Somebody)}
\newcommand*{\checkmissing}[1]{\DTLifnull{#1}{}{#1}}
\newcommand{\PrintDocTable}[3][]{%
% #1 = list of rowIDs
% #2 = database to search
% #3 =caption
\begin{longtable}{r l p{1.5in} c c p{2.5in}}
\caption{#3}\\
& \colhead{Date} & \colhead{Filename} & \colhead{From} & \colhead{To} & \colhead{Subject}\\\hline\endhead
\DTLforeach
[\ifblank{#1}{\boolean{true}}{\DTLisSubString{#1}{\RowID}}]
{#2}{%
\RowID=RowID,%
\Date=Date,%
\Filename=Filename,%
\From=From,%
\To=To,%
\Subject=Subject%
}{%
\nextnuml{\RowID} & \Date & {\bfseries\expandafter\url\expandafter{\Filename} } & \checkmissing{\From} & \checkmissing{\To} & \Subject \\
}%
\end{longtable}
}%
\begin{filecontents*}{doc.csv}
familytree,26 Feb 2014,something.txt,subject
2013.10.05.kanjur,05 Oct 2013,something.txt,subject
2013.10.05.powai,05 Oct 2013,something.txt,subject
2014.11.14,14 Nov 2014,something.txt,FROM,TO,,subject
\end{filecontents*}
\begin{document}
\def\today{9th January, 2014}
\begin{letter}{
Someone\\
Somewhere\\
Subject: Some stuff
}
\opening{Dear Someone}
Here are some refs - [\ref{2014.11.14}]. [\ref{2013.10.05.powai}].
\closing{Yours Sincerely,}
\DTLloaddb[noheader,keys={RowID,Date,Filename,From,To,Email,Subject}]{dDB}{doc.csv}
%\PrintDocTable{dDB}{Documents}
\PrintDocTable[2014.11.14,2013.10.05.powai]{dDB}{Documents}
\end{letter}
\end{document}
答案1
像这样吗?
\documentclass{letter}
\usepackage[verbose]{datatool}
\usepackage{longtable}
\usepackage{array}
\usepackage{url}
\newcounter{tabenum}\setcounter{tabenum}{0}
\newcommand{\colhead}[1]{\multicolumn{1}{>{\bfseries}l}{#1}}
\newcommand{\nextnuml}[1]{\refstepcounter{tabenum}\thetabenum.\label{#1}}
\makeatletter
\AtBeginDocument{\providecommand\ReferencedIDs{}}
\AtEndDocument{\immediate\write\@auxout{\gdef\string\ReferencedIDs{\ReferencedIDs}}}
\let\oldref\ref
\def\ref#1{%
\g@addto@macro\ReferencedIDs{,#1}%
\oldref{#1}%
}
\makeatother
\usepackage{fouriernc}
\address{Some Address\\ Some Place\\Email: [email protected]}
\signature{(Somebody)}
\newcommand*{\checkmissing}[1]{\DTLifnull{#1}{}{#1}}
\newcommand{\PrintDocTable}[3][]{%
% #1 = list of rowIDs
% #2 = database to search
% #3 =caption
\begin{longtable}{r l p{1.5in} c c p{2.5in}}
\caption{#3}\\
& \colhead{Date} & \colhead{Filename} & \colhead{From} & \colhead{To} & \colhead{Subject}\\\hline\endhead
\DTLforeach
[\ifblank{#1}{\boolean{true}}{\DTLisSubString{#1}{\RowID}}]
{#2}{%
\RowID=RowID,%
\Date=Date,%
\Filename=Filename,%
\From=From,%
\To=To,%
\Subject=Subject%
}{%
\nextnuml{\RowID} & \Date & {\bfseries\expandafter\url\expandafter{\Filename} } & \checkmissing{\From} & \checkmissing{\To} & \Subject \\
}%
\end{longtable}
}%
\begin{filecontents*}{doc.csv}
familytree,26 Feb 2014,something.txt,subject
2013.10.05.kanjur,05 Oct 2013,something.txt,subject
2013.10.05.powai,05 Oct 2013,something.txt,subject
2014.11.14,14 Nov 2014,something.txt,FROM,TO,,subject
\end{filecontents*}
\begin{document}
\def\today{9th January, 2014}
\begin{letter}{
Someone\\
Somewhere\\
Subject: Some stuff
}
\opening{Dear Someone}
Here are some refs - [\ref{2014.11.14}]. [\ref{2013.10.05.powai}].
\closing{Yours Sincerely,}
\DTLloaddb[noheader,keys={RowID,Date,Filename,From,To,Email,Subject}]{dDB}{doc.csv}
%\PrintDocTable{dDB}{Documents}
\PrintDocTable[\ReferencedIDs]{dDB}{Documents}\renewcommand\ReferencedIDs{}
\end{letter}
\end{document}