# Reference list from file for citations

I try to automate part of the work linked to a set of figures. The figures are generated from data files, and I would like to add the source of the data for each figure, from a file containing the citation key.

Here is a MWE focussing on this citation feature:

% !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",
}
@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} print ref1,ref2 in the pdf as expected.

\cite{ref1,ref2} creates the correct references in the PDF.

\cite{\input{biblist1}} generates an error:

! 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


Any idea on how I should load the files?

• The default file extension in the argument of \input is .tex, not .txt. More importantly, the argument of \cite (as defined by the cite package) is not expanded; it’s supposed to be a list of keys to BibTeX entries. – Mico Jul 23 '18 at 20:28
• @Mico I changed to .tex in the MWE. My level in *tex do not allow me to understand the expanding issue you refer to. – Togh Jul 23 '18 at 20:36

\input is not the right tool; you can use catchfile instead to save the file contents in a macro that you can then expand at the right time.

\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",
}
@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}


I added \jobname- as a prefix in order not to clobber my files, it's not required for the solution.

A simpler version with expl3 (no \expandafter, but the idea is the same).

\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",
}
@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}

• Nice, and thanks to you I understood the point of the \jobname. Still a small issue remaining. It seems that when reading the file, lualatex sees a space at the end (while the tool I'm using do not intend to print it). I guess this comes from encoding issue. Is there a way to trim the last character from the file? BibTeX really do not like this space. – Togh Jul 23 '18 at 20:55
• @Togh \CatchFileDef{\filecitetemp}{#1}{\endlinechar=-1 } – egreg Jul 23 '18 at 21:04
• The issue comes from my files on the real work case, and is not replicated with this mwe as filecontents does a great job. Your answer fully comply with the question I asked. I generate these files from Julia's write() function. If you think this should require a new question or be dealt on the Julia side, don't hesitate to tell. – Togh Jul 23 '18 at 21:13
• Perfect! This \endlinechar=-1` is the solution to my encoding issue. – Togh Jul 23 '18 at 21:35