1

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:

help icon with wrong text

This is not the thing, I intended. The comment should have the full text of the file.

Solution Thoughts

koppor
  • 3,252

1 Answers1

2

Actually easy - i am just not sure if one can easily include the linebreaks, but i am sure you are going to find out. Using expl3 because it's much more readable in there ;-)

\documentclass{article}

\usepackage{pdfcomment}

\begin{filecontents}{demo.bib} @article{demo, author = {demo} } \end{filecontents}

\begin{document}

\ExplSyntaxOn \file_get:nnN {demo.bib} { \char_set_catcode_other:N _ \char_set_catcode_other:N $ \char_set_catcode_other:N { \char_set_catcode_other:N} } \l_tmpa_tl

\exp_args:NnV \use:n {\pdfcomment[icon=Help]}{\l_tmpa_tl}

\ExplSyntaxOff \end{document}

And just in case you don't want to change all characters to other manually you could also iterate over a squence and read the file as a string:

\documentclass{article}

\usepackage{pdfcomment}

\begin{filecontents}{demo.bib} @article{demo, author = {demo} } \end{filecontents}

\begin{document}

\ExplSyntaxOn \ior_new:N \l_tmpa_ior \ior_open:Nn \l_tmpa_ior {demo.bib} \seq_clear:N \l_tmpa_seq \ior_str_map_inline:Nn \l_tmpa_ior { \seq_put_right:Nn \l_tmpa_seq {#1} }

\exp_args:Nnx \use:n {\pdfcomment[icon=Help]}{\seq_use:Nn \l_tmpa_seq {\textLF}} \ExplSyntaxOff \end{document}

Marei
  • 211
  • 1
    Use cctab_select:N \c_other_cctab to 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