GeneralNeed to replace newline with '\textLF' (on text imported with \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

Need to replace newline with '\textLF' (on text imported with \input)

Post by cypherpunks »

This thread describes how to input a text file and put the contents into a PDF annotation using the \pdfcomment command. Problems occur when the input text contains a blank line because \pdfcomment cannot handle it. According to the pdfcomment docs, linefeeds must be replaced with \textLF. I tried using \StrSubstitute:

Code: Select all

\makeatletter
\gdef\pdfcommentfile#1{%
  \begingroup
  \everyeof{\noexpand}%
  \long\edef\temp{\noexpand\pdfcomment{\StrSubstitute{\@@input{#1}}{\noexpand\noexpand\noexpand\newline}{\textLF}}}%
  \temp
  \endgroup
}%
\makeatother
This does not compile. It gives:
ERROR: Illegal parameter number in definition of \temp.
Any ideas how to overcome this?

Note as well that there are other characters that \pdfcomment chokes on, like “&” and “%”, so this code needs to accommodate multiple replacements. In fact, this must be a common problem whenever importing external text. Is there a package that generally sanitizes input text for LaTeX?

Here is some sample broken code:

Code: Select all

\documentclass{article}

\usepackage{pdfcomment}
\usepackage{newfile}

% (heredoc) generate a text file that has a blank line, to feed back in as sample input:
\newoutputstream{sampleinput}
\openoutputfile{\jobname_sample.txt}{sampleinput}
\addtostream{sampleinput}{line 1}
\addtostream{sampleinput}{} % blank line
\addtostream{sampleinput}{line 2}
\closeoutputstream{sampleinput}

% Furnish a \pdfcommentfile command which takes a filename as input and feeds the contents of the file to \pdfcomment:
\makeatletter
\gdef\pdfcommentfile#1{%
  \begingroup
  \everyeof{\noexpand}%
  \long\edef\temp{\noexpand\pdfcomment{\@@input{#1}}}%
  \temp
  \endgroup
}%
\makeatother

\begin{document}
\pdfcommentfile{\jobname_sample.txt}
\end{document}
To make that code function, this line must be removed:

Code: Select all

\addtostream{sampleinput}{} % blank line
Of course text input will often have blank lines, so LaTeX code is needed to replace the blank lines with \textLF.

(update) This shows a working implementation but it’s ugly as fuck because text must be written to a temporary file then read back in from the file:

Code: Select all

\documentclass{article}

\usepackage{mwe}
\usepackage{pdfpages}
\usepackage{pdfcomment}
\usepackage{newfile}
\usepackage{xstring}
\usepackage{catchfile}

% heredoc holding text that normally breaks the \pdfcomment command:
\begin{filecontents*}{\jobname_sample.txt}
line one

line two
tricky symbols: _&%
\end{filecontents*}

% Create \pdfcommentfile, which is a version of \pdfcomment that can read from a file:
\makeatletter
\gdef\pdfcommentfile#1{%
  \begingroup
  \everyeof{\noexpand}%
  \long\edef\temp{\noexpand\pdfcomment{\@@input{#1}}}%
  \temp
  \endgroup
}%
\makeatother

\CatchFileDef{\cfile}{\jobname_sample.txt}{} % side-effects: replaces blank lines with “\par” and drops percent symbols

% Replace blank lines with \textLF and replace special symbols with those that are safe for \pdfcomment. Warning: this is probably not a complete list of all constructs that break \pdfcomment!
\StrSubstitute{\cfile}{\par}{\string\noexpand\string\textLF\ }[\pdfannotationtxt]
\StrSubstitute{\pdfannotationtxt}{\%}{\string\noexpand\string\%}[\pdfannotationtxt]
\StrSubstitute{\pdfannotationtxt}{_}{\string\noexpand\string\_}[\pdfannotationtxt]
\StrSubstitute{\pdfannotationtxt}{&}{\string\noexpand\string\&}[\pdfannotationtxt]

% the \pdfcomment command cannot directly handle the above substitutions (nor can it handle the original unsubstituted version). So we write the new version to another file:

\newoutputstream{filteredresult}
\openoutputfile{\jobname_filtered.txt}{filteredresult}
\addtostream{filteredresult}{\pdfannotationtxt}
\closeoutputstream{filteredresult}

\begin{document}
\pdfcommentfile{\jobname_filtered.txt}
\includepdf{example-image-a.pdf}
\end{document}

Recommended reading 2024:

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