General[solved] unable to use \pdfcomment on text imported from an external file (\input)

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
cypherpunks
Posts: 15
Joined: Sat Dec 10, 2016 12:26 pm

[solved] unable to use \pdfcomment on text imported from an external file (\input)

Post by cypherpunks »

Here is a minimal example:

Code: Select all

\documentclass{article}

\usepackage{pdfcomment}

\begin{document}
yada yada

\pdfcomment{This is a PDF annotation -- works as expected.}

\pdfcomment{\input{some_text.txt}} % the PDF comment is literally the text “some_text.txt”

yada yada
\end{document}
It compiles fine but the result is that the name of the external text file is itself used as the text of the annotation. I also have the solution in case anyone needs this:

Code: Select all

\documentclass{article}

\usepackage{pdfcomment}

\makeatletter
\gdef\pdfcommentfile#1{%
  \begingroup
  \everyeof{\noexpand}%
  \long\edef\temp{\noexpand\pdfcomment{\@@input{#1}}}%
  \temp
  \endgroup
}%
\makeatother

\begin{document}
yada yada

\pdfcommentfile{some_text.txt}
\end{document}
That’s not my code. It came from someone more advanced than me -- and should probably be added to the pdfcomment package.

(edit) Just discovered a limitation: If the source text contains \textLF, then it is seen as an undefined control sequence. So in input files need to filter through sed code:

Code: Select all

${/^$/d}
/^$/{N; s/\n/\\noexpand\\textLF /gi;}
s/%/\\%/gi
s/&/\\&/gi
s/_/\\textunderscore/gi

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
Post Reply