I want to have file contents rendered as comment in a PDF file. I decided for pdfcomment, which works. pdfcomment requires newlines being typeset as \textCR\textLF. Which leas me to the following setting:
I would like to read the contents of a file, replace a newline by \textCR\textLF and have this passed to pdfcomment. As additional challenge, character sequences such as {, \_, .. need to be preserved.
Rough example:
demo.bib:
@article{demo,
author = {Demo Frühling},
comment = {Fr\"{u}hling}
}
After reading the file, following text should be passed to pdfcomment:
@article{demo,\textCR\textLF
author = {Demo Frühling},\textCR\textLF
comment = {Fr\"{u}hling}\textCR\textLF
}\textCR\textLF
Longer desired result:
\pdfcomment[icon=Help]{
@article{demo,\textCR\textLF
author = {Demo Frühling},\textCR\textLF
comment = {Fr\"{u}hling}\textCR\textLF
}\textCR\textLF}
Example with (non-working) code
\documentclass{article}
\usepackage{pdfcomment}
\newread\reader
\begin{document}
\begin{filecontents}{demo.bib}
@article{demo,
author = {Demo Frühling},
comment = {Fr"{u}hling}
}
\end{filecontents}
\pdfcomment[icon=Help]{
\makeatletter
\advance\endlinechar @M
\openin%
\reader=demo.bib%
\relax
\loop
\read\reader to \x
\unless\ifeof\reader
\x \textCR\textLF
\repeat
\closein\reader
\advance\endlinechar -@M
\makeatother
}
\end{document}
That renders to following:
This is not the thing, I intended. The comment should have the full text of the file.
Solution Thoughts
- I played around with catchfile, but with no success. (Related question: \CatchFileDef vs ( \input or \@@input ))
- I think datatool with
\DTLforeachcould work somehow, but I don't how to read a non-CSV file with data tool - readarray looks good with
readrecordarray, but I don't know how to craft aforloop, especially the end condition is unclear to me. Related answer: https://tex.stackexchange.com/a/116901/9075

cctab_select:N \c_other_cctabto be completely safe. (ah, I have a question https://tex.stackexchange.com/q/647304/250119 on how to read whole file and some additional performance discussion) – user202729 Jul 22 '22 at 11:58