2

I have a command:

\newcommand{\help}[2]{
  \href{https://www.rdocumentation.org/packages/#1/functions/#2}{\texttt{?\detokenize{#2}}}
}

and another

\newcommand{\code}[1]{\texttt{\detokenize{#1}}}

They don't work with

\help{magritrr}{%>%}

or

\code{%>%}
1
  • Commands like \url change the catcode of % at execution and change back after they finish the job. Maybe you can do the same thing.
    – Symbol 1
    Mar 4 at 15:46
2

As @Symbol 1 suggested in a comment, changing the catcode of % before the argument is absorbed is essential. And then making sure it is changed back when done, is equally essential.

\documentclass{article}
\newcommand{\code}{\catcode`\%=12 \codeaux}
\newcommand\codeaux[1]{\texttt{\detokenize{#1}}\catcode`\%=14}
\begin{document}
\code{%>%}

Test% if these print or not
\end{document}

enter image description here

The alternative approach is to use grouping to negate the catcode change upon exit:

\documentclass{article}
\newcommand{\code}{\begingroup\catcode`\%=12 \codeaux}
\newcommand\codeaux[1]{\texttt{\detokenize{#1}}\endgroup}
\begin{document}
\code{%>%}

Test% if these print or not
\end{document}
7
  • 1
    it should be \newcommand\codeaux[1]{\endgroup\texttt{\detokenize{#1}}}. Your version doesn't close the group. Mar 4 at 16:04
  • Should there be an \endgroup in codeaux? If so, the change back is unnecessary. Also, why not simply use \verb".."? Mar 4 at 16:07
  • @HerbSchulz Yes, thanks. Mar 4 at 16:10
  • @UlrikeFischer yes, will fix Mar 4 at 16:10
  • So is this then the response to the first part of my question: ```\newcommand{\help}[2]{\href{rdocumentation.org/packages/#1/functions/#2}{\code{?}\code{#2}}}}``
    – mk9y
    Mar 5 at 1:35

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.