# How to replace blank lines by newline character?

I would like to replace blank lines in a command by the newline character \textCR.

For example:

\documentclass{article}
\usepackage{pdfcomment}

\mypdfcomment[2][]{\pdfcomment[#1]{#2}}

\begin{document}
\mypdfcomment{first line

second line}
\end{document}


Here I would like

\mypdfcomment{first line

second line}


to produce

\pdfcomment{first line\textCR second line}


Background: pdfcomment does not understand blank lines and instead uses \textCR. It would be good not having to care about.

• Please make a complete example, including the definition of \mypdfcomment Sep 23, 2016 at 8:19
• @egreg The idea was just to have \mypdfcomment do the same as \pdfcomment except for the replacement of the empty lines. Maybe you know what is going wrong in trying to use the solution below to replace \newline and \ as well (see comments)? Sep 23, 2016 at 9:23

Like this?

\documentclass{article}
\usepackage{pdfcomment}

\newcommand\mypdfcomment[2][]{{%
\let\par\textCR
\pdfcomment[#1]{#2}%
}}

\begin{document}
\mypdfcomment{first line

second line}
\end{document}

• Thanks a lot! And my limited LaTeX knowledge was enough to recover the optional argument. So I am using \newcommand{\mypdfcomment}[2][]{\bgroup \let\par\textCR \pdfcomment[#1]{#2}\egroup}. Is there a particular reason you introduced the domypdfcomment? Or was it just to make the code clearer? Sep 23, 2016 at 8:49
• I realised that there are a couple of additional line breaks in LaTeX to take care of. I tried to include them like this: \newcommand{\mypdfcomment}[2][]{\bgroup \let\par\textCR \let\newline\textCR \let\\\textCR \pdfcomment[#1]{#2}\egroup}. But it doesn't work. Any idea on this? Sep 23, 2016 at 9:04
• @Daniel No idea why this does not work. I edited my answer to reflect your desired solution. The two-stage macro was a leftover from a previous attempt, I removed it. Sep 23, 2016 at 9:16
• My guess is that it does not work because \pdfcomment already removes the \newline somehow. Notice that a blank line in \pdfcomment leads to en error while \newline does not and makes no change. Sep 23, 2016 at 11:24