I'm writing a text about programming and want to include some inline small code snippets. In markdown one would simply use backticks for this test
. This automatically becomes monospace font. I want a simple command for this. (Backticks would even be better)
I tried using:
\newcommand{\mon}[1]{
\mbox{\texttt{\detokenize{#1}}}
}
My command is \mon
with one parameter, the \texttt is for the monospace and detokenize is for characters which would otherwise be parsed by Latex.(Like $, & etc) However this doesn't work for #
. If I want to use \mon{F#}
I get this error:
Illegal parameter number in definition of \reserved@a. \mon{F#}
Here is a test document:
\documentclass{article}
\newcommand{\mon}[1]{
\mbox{\texttt{\protect\detokenize{#1}}}
}
\begin{document}
\mon{F#}
\end{document}
I also tried with using a custom environment. I guess it must be possible since lstlistings
is doing it.
Thanks
Best Answer
You can possibly do it with
\verb
. But let's look what happens with your attempt. Inthere is a misplaced
\protect
that however does nothing bad. The problem is that TeX sees#
6 before the “detokenization” actually takes place and this results in a bad token list to be passed as argument to\texttt
. Indeed, if you use\ttfamily
the problem is solved:A different strategy could be performing
\detokenize
before doing anything else:This however doesn't solve the problem with
F#
, because you'll clearly see the#
is doubled. That's part of how\detokenize
works (and TeX is designed), so for this particular application I'm afraid\verb
is the solution. Or some less heavy trick that changes the category code of#
(so it can't be used in the argument to another command).A different approach might be with
expl3
:The assumption is that any
##
in the token list we build comes from\tl_to_str:n
(which is\detokenize
in disguise) applied to a#
6 token, so it's replaced by a single one.This has still some limitations, because it can't go in the argument of any argument that performs some
\protected@edef
or\protected@write
; for instance\textbf
and similar commands or\section
,\caption
and friends.