7

I am writing some personal notes about R programming. I defined a macro to typeset R functions and operators using a different font style and color:

\newcommand{\rtext}[1]{%
  {\texttt{\detokenize{#1}}}%
}

The macro is able to detokenize underscore characters _ so that I can easily type R functions such as \rtext{seq_along}.

Today I found that the macro fails at detokenizing the percent character %, for example when I try to typeset the %>% operator which is from the R package magrittr. I understand that this is because the percent character is used to mark the start of a comment. Unfortunately, trying to use \rtext{\%>\%} gives \%>\% as output, which is not what is desired.

What is the right way to define the \rtext macro?

\documentclass{article}
\newcommand{\rtext}[1]{%
  {\texttt{\detokenize{#1}}}%
}
\begin{document}
You can write commands in a natural order
by using the \rtext{%>%} infix operator.
\end{document}

Gives the error:

Runaway argument?
{\end {document} 
! File ended while scanning use of \rtext.
<inserted text> 
                \par 

Edit in response to answers: I added the use of \rtext in a sentence. Unfortunately the answers provided seem to swallow the space after the command. Is there any way to fix this?

  • Why don't you use \verb? – cgnieder Apr 25 '15 at 20:04
  • If I were to use \verb, I wouldn't have the text in the color that I want, e.g. blue text in the font size \small. – I Like to Code Apr 25 '15 at 20:40
  • @ILiketoCode: You could change that... – Werner Apr 25 '15 at 20:42
  • @ILikeToCode IIRC you could redefine \verbatim@font – cgnieder Apr 25 '15 at 21:07
  • This \documentclass{article} \usepackage{verbatimbox,xcolor} \begin{document} text {\color{blue}\begin{verbbox}[\small\rmfamily] My text \end{verbbox} \theverbbox} text \end{document} will give small blue (rmfamily) verbatim font, inline. Just make sure you start the verbatim text on a new line, after closing the optional argument. – Steven B. Segletes Apr 25 '15 at 23:59
4

You can do some catcode magic. The general idea is as follows

\documentclass{article}

\makeatletter
\newcommand\detokenizeWithComments{%%
  \bgroup
    \catcode`\%=12
    \ae@detokenize@with@comments
  }


\def\ae@detokenize@with@comments#1{%%
    \detokenize{#1}%%
  \egroup}

\makeatother

\begin{document}

  Hello world
  \detokenizeWithComments{This has % in it}

  %% back to normal

  back to normal (shouldn't be repeated)

  %% but this next line will fail if uncommented!!!
  %%\texttt{\detokenizeWithComments{This has % in it}}

\end{document}

enter image description here

To get your \rtext to work, you can approach it in this manner:

\documentclass{article}

\makeatletter

\newcommand\rtext{%%
  \bgroup
    \catcode`\%=12
    \ae@r@text
}

\def\ae@r@text#1{%%
  \texttt{\detokenize{#1}}%%
\egroup}

\makeatother

\begin{document}

  Detokenized: \rtext{{This has % in it}}

  Back to normal

\end{document}

As noted by @egreg This macro will not play nicely within another macro or environment that has already read in the arguments. This is similar to the issue with \verb not working nested within other macros. The catcodes are alrady set and the % is already seen as a comment character before the catcode magic ever has a chance to take effect: not even \scantokens can come to the rescue here. And hence the fact that I couldn't just define:

\newcommand\rtext[1]{\texttt{\detokenizeWithComments{#1}}

If you try that, you'll just get the same error you originally had.

Regarding category codes, you could set the category code to 11 for letters as I originally did in this answer. But since \detokenize sets the category codes to 12, setting

\catcode`\%=12 

makes an aesthetically cleaner choice.

|improve this answer|||||
  • With the note that this \rtext command can't be used in the argument to another command. This is unavoidable, unless we change the category code of % globally (to category code 12, please). – egreg Apr 25 '15 at 20:08
3

Similar to A.Ellett's answer: Switch the category code of % from "comment character" to "other" and back again:

enter image description here

\documentclass{article}

\makeatletter
% Switch catcode for % to other
\newcommand{\rtext}{%
  \catcode`\%=12
  \@rtext}
% Switch catcode for % back to comment character
\newcommand{\@rtext}[1]{\texttt{\detokenize{#1}}\catcode`\%=14}
\makeatother

\begin{document}
\rtext{%>%}
\end{document}

Note that one could use comment characters in the definition of \@rtext in order to make the macro code more readable despite the fact that you've changed the \catcode of % to be "other" (and therefore printable):

\newcommand{\@rtext}[1]{%
  \texttt{\detokenize{#1}}%
  \catcode`\%=14}

The reason for this is because the category codes are fixed at definition time, and therefore % still represents a comment character inside \@rtext.

|improve this answer|||||
  • \begingroup and \endgroup are much better. And category code 12 too. – egreg Apr 25 '15 at 20:05
  • @egreg Why category code 12? And why \begingroup instead of \bgroup....? – A.Ellett Apr 25 '15 at 20:07
  • @A.Ellett \bgroup and \egroup surely work. Personal preference. Category code 12 is for “other characters”. Not a big deal here, because \detokenize will transform everything except spaces into category code 12. – egreg Apr 25 '15 at 20:11
  • @egreg Well, that makes sense to me. I'll change that in my answer. – A.Ellett Apr 25 '15 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.