5

I would like to include reserved characters such as _ within comments so that I can include file links in comments.

If I try to enclose a sentence in a verbatim environment within a pdfcomment environment, such as this

\pdfcomment{\begin{verbatim}F:\images_test\20140105\DSC_0175.JPG\end{verbatim}}

I get an error saying that the reserved character can't be used in a pdf comment.

How can I do this? I would prefer not to escape individual reserved characters one at a time (e.g. \_) as it makes the comments hard to read in the latex code itself, although I know this can work.

Here is a MWE that does not compile in latex.

\documentclass{article}
\usepackage{pdfcomment}
\usepackage{verbatim}
\begin{document}
test\pdfcomment{\begin{verbatim}C:\User_test\end{verbatim}}

test\pdfcomment{C:\User_test}
\end{document}
8
  • 2
    The underscore is no problem. Take the verbatim environment away and that works fine. So if you used \backslash for \, I guess it would work. Although this doesn't show a pop-up in my viewer (but neither does the documentation). If you switch to GNU/Linux, your paths wouldn't have this problem!
    – cfr
    Jan 7, 2014 at 4:01
  • You need to indent code by 4 spaces. Usually, we don't put a greeting or a “thank you” in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you. Jan 7, 2014 at 4:17
  • @cfr -- The underscores will also be a problem. pdfcomment relies on \pdfstringdef from hyperref.
    – jon
    Jan 7, 2014 at 5:45
  • Can you add a minimal working example (MWE)?
    – egreg
    Jan 7, 2014 at 12:04
  • @jon OK. It worked fine for me when I tested it but maybe it causes other issues later on. (That is, when I just removed the verbatim environment and changed to unix-style paths, I got no errors and I got a pop-up as expected.)
    – cfr
    Jan 7, 2014 at 15:07

1 Answer 1

5

If you don't plan to use \pdfcomment as argument to another command, this will work:

\documentclass{article}
\usepackage{pdfcomment}
\makeatletter
\newcommand{\PDFcomment}{%
  \begingroup
  \begingroup\lccode`\~=`\\ 
    \lowercase{\endgroup\let~\textbackslash}%
  \let\do\@makeother\dospecials
  \catcode`\{=1 \catcode`\}=2 \catcode`\\=\active
  \PDF@comment}
\def\PDF@comment#1{\pdfcomment{#1}\endgroup}
\makeatother


\begin{document}
\tracingmacros=1
test\PDFcomment{C:\User_test}
\tracingmacros=0

\end{document}

enter image description here

4
  • Thanks @egreg. When I run this the code compiles without errors, which is progress, but the \ characters disappear in the comment. Do you find this?
    – Chogg
    Jan 9, 2014 at 1:31
  • @Chogg Here's a fixed version.
    – egreg
    Jan 9, 2014 at 10:44
  • I run into difficulties if I try to use options. I assume that I need to change the line \def\PDF@comment#1{\pdfcomment{#1}\endgroup} to something like \def\PDF@comment#1#2{\pdfcomment[#1]{#2}\endgroup} but I don't really know how to write this code. Is this correct?
    – Chogg
    Jan 15, 2014 at 19:08
  • @Chogg Please, add an example with options.
    – egreg
    Jan 15, 2014 at 19:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .