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?


\verb? – cgnieder Apr 25 '15 at 20:04\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\verbatim@font– cgnieder Apr 25 '15 at 21:07\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\verbatim@font? – I Like to Code Apr 26 '15 at 06:00