我尝试将与一组图表相关的部分工作自动化。图表是从数据文件生成的,我想从包含引用关键字的文件中为每个图表添加数据来源。
以下是重点关注此引用特征的 MWE:
% !TEX program = lualatex
% !TeX encoding = UTF-8
\documentclass{article}
\usepackage{filecontents}
\usepackage{cite}
\begin{filecontents}{biblist1.tex}
ref1,ref2
\end{filecontents}
\begin{filecontents}{biblist2.tex}
ref2,ref3
\end{filecontents}
\begin{filecontents}{sample.bib}
@article{ref1,
author = "Albert Einstein",
title = "{Zur Elektrodynamik bewegter K{\"o}rper}. ({German})
[{On} the electrodynamics of moving bodies]",
journal = "Annalen der Physik",
volume = "322",
number = "10",
pages = "891--921",
year = "1905",
DOI = "http://dx.doi.org/10.1002/andp.19053221004"
}
@book{ref2,
author = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
title = "The \LaTeX\ Companion",
year = "1993",
publisher = "Addison-Wesley",
address = "Reading, Massachusetts"
}
@misc{ref3,
author = "Donald Knuth",
title = "Knuth: Computers and Typesetting",
url = "http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html"
}
\end{filecontents}
\begin{document}
Test document.
% Here is the test:
Sources set 1 are: \cite{\input{biblist1}}
Sources set 2 are: \cite{\input{biblist2}}
\bibliographystyle{plain}
\bibliography{sample}
\end{document}
\input{biblist1}
ref1,ref2
按预期在 pdf 中打印。
\cite{ref1,ref2}
在 PDF 中创建正确的引用。
\cite{\input{biblist1}}
产生错误:
! Incomplete \iffalse; all text was ignored after line 46.
<inserted text>
\fi
<*> TestBib.tex
The file ended while I was skipping conditional text.
This kind of error happens when you say `\if...' and forget
the matching `\fi'. I've inserted a `\fi'; this might work.
! Emergency stop.
<*> TestBib.tex
关于我应该如何加载文件有什么想法吗?
答案1
\input
不是合适的工具;您可以改用catchfile
宏来保存文件内容,然后在适当的时候进行扩展。
\begin{filecontents}{\jobname-biblist1.tex}
ref1,ref2
\end{filecontents}
\begin{filecontents}{\jobname-biblist2.tex}
ref2,ref3
\end{filecontents}
\begin{filecontents}{\jobname.bib}
@article{ref1,
author = "Albert Einstein",
title = "{Zur Elektrodynamik bewegter K{\"o}rper}. ({German})
[{On} the electrodynamics of moving bodies]",
journal = "Annalen der Physik",
volume = "322",
number = "10",
pages = "891--921",
year = "1905",
DOI = "http://dx.doi.org/10.1002/andp.19053221004"
}
@book{ref2,
author = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
title = "The \LaTeX\ Companion",
year = "1993",
publisher = "Addison-Wesley",
address = "Reading, Massachusetts"
}
@misc{ref3,
author = "Donald Knuth",
title = "Knuth: Computers and Typesetting",
url = "http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html"
}
\end{filecontents}
\documentclass{article}
\usepackage{cite}
\usepackage{catchfile}
\newcommand{\filecite}[1]{%
\CatchFileDef{\filecitetemp}{#1}{\endlinechar=-1 }%
\expandafter\cite\expandafter{\filecitetemp}%
}
\begin{document}
Test document.
% Here is the test:
Sources set 1 are: \filecite{\jobname-biblist1}
Sources set 2 are: \filecite{\jobname-biblist2}
\bibliographystyle{plain}
\bibliography{\jobname}
\end{document}
我添加了\jobname-
一个前缀以避免破坏我的文件,但这对于解决方案来说不是必需的。
一个更简单的版本expl3
(没有\expandafter
,但是想法是一样的)。
\begin{filecontents}{\jobname-biblist1.tex}
ref1,ref2
\end{filecontents}
\begin{filecontents}{\jobname-biblist2.tex}
ref2,ref3
\end{filecontents}
\begin{filecontents}{\jobname.bib}
@article{ref1,
author = "Albert Einstein",
title = "{Zur Elektrodynamik bewegter K{\"o}rper}. ({German})
[{On} the electrodynamics of moving bodies]",
journal = "Annalen der Physik",
volume = "322",
number = "10",
pages = "891--921",
year = "1905",
DOI = "http://dx.doi.org/10.1002/andp.19053221004"
}
@book{ref2,
author = "Michel Goossens and Frank Mittelbach and Alexander Samarin",
title = "The \LaTeX\ Companion",
year = "1993",
publisher = "Addison-Wesley",
address = "Reading, Massachusetts"
}
@misc{ref3,
author = "Donald Knuth",
title = "Knuth: Computers and Typesetting",
url = "http://www-cs-faculty.stanford.edu/\~{}uno/abcde.html"
}
\end{filecontents}
\documentclass{article}
\usepackage{cite}
\usepackage{xparse}
\ExplSyntaxOn
% internal version of \cite
\cs_new_eq:NN \togh_cite:n \cite
\cs_generate_variant:Nn \togh_cite:n { V }
\tl_new:N \l_togh_cite_tl
\NewDocumentCommand{\filecite}{m}
{
\tl_set_from_file:Nnn \l_togh_file_tl { } { #1 }
\togh_cite:V \l_togh_file_tl
}
\ExplSyntaxOff
\begin{document}
Test document.
% Here is the test:
Sources set 1 are: \filecite{\jobname-biblist1}
Sources set 2 are: \filecite{\jobname-biblist2}
\bibliographystyle{plain}
\bibliography{\jobname}
\end{document}