0

This is related to Simplify the use of ampersand & in glossaries description, but having read those answers and tried something based on them, I'm finding it still doesn't work for my case.

Basically I'm trying to make a macro \code{...} that will typeset ... in monospace font, inline. I guess I'm looking for something fairly similar to WEB's |...| syntax, except that | would be a fairly bad choice of delimiter character for my purposes.

For example, I want this:

To capture a reference to the local variable \code{snake_case},
we write \code{[&snake_case]}.

to typeset as if I'd written

To capture a reference to the local variable \texttt{snake\_case},
we write \texttt{[\&snake\_case]}.

I'm going to be writing a lot of inline code snippets, so I'd prefer not to have to remember to backslash-escape any special character if it's at all avoidable.

My attempt so far is:

% make _ a non-special character
\usepackage{underscore}

% make & a non-special character
\newcommand{\makeampletter}{\catcode`\&=12\relax}
\newcommand{\makeampother}{\catcode`\&=4\relax}

\newcommand{\code}[1]{\makeampletter\texttt{#1}\makeampother}

I am vaguely aware of the existence of \verb|...|, but answers like When should one use \verb and when \texttt have made me wary of it; I'd rather use something that acts as much like \texttt as possible, and where I 100% understand how I'm deviating from \texttt when I use it.

(Bonus points for not only fixing my ampersand issue but also addressing my inevitable future issues with %, {, and }.)

  • any command based on catcode changes has exactly the same issues as verb so you may as well use verb – David Carlisle Mar 1 '17 at 0:17
  • @DavidCarlisle: I've heard that \verb also messes with hyphenation and line-breaking; and that it prints underscores weirdly. – Quuxplusone Mar 1 '17 at 0:19
  • Nerfs ? What am I missing ? – user103221 Mar 1 '17 at 0:21
  • don't believe everything you hear. The disabling of hyphenation is a feature of the monospace font setup so applies equally to \verb and \texttt the issues with verb not working on arguments of other commands are just related to catcode. if you fix the & catcode change in your definition and do the same for all the other special characters then you will just have the definition of \verb. – David Carlisle Mar 1 '17 at 0:26
  • 2
    @RobtA: Try adding \usepackage{listings} \lstset{basicstyle=\ttfamily} \let\code\lstinline to your preamble... – Werner Mar 1 '17 at 0:39
1

Thanks to Werner in the comments for this:

\newcommand{\code}[1]{\lstinline[basicstyle=\ttfamily,autodedent=0]{#1}}

(Notice that autodedent is being provided for me by Jubobs' lstautodedent package.)

My only remaining problems with my \code have to do with linebreaking. Namely,

  • sometimes I see foo, \code{bar}, baz linebreaking between "bar" and ","
  • sometimes I see \code{foo, bar, baz} linebreaking between "bar" and ","

The former is indefensible; the latter is at least defensible, even if it's ugly. I suspect there are things I could do to improve this answer in those departments.

  • 1
    Also add breaklines=false to your list of options. – Werner Mar 2 '17 at 6:51
  • @Werner: breaklines=false fixes the latter problem but not the former, right? I actually already fixed the latter by throwing an \mbox{...} around the whole thing, although I might change it to breaklines=false now. – Quuxplusone Mar 2 '17 at 7:02

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.