1

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 01 '17 at 00:17
  • @DavidCarlisle: I've heard that \verb also messes with hyphenation and line-breaking; and that it prints underscores weirdly. – Quuxplusone Mar 01 '17 at 00:19
  • Nerfs ? What am I missing ? –  Mar 01 '17 at 00: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 01 '17 at 00:26
  • 2
    @RobtA: Try adding \usepackage{listings} \lstset{basicstyle=\ttfamily} \let\code\lstinline to your preamble... – Werner Mar 01 '17 at 00:39
  • @Werner: That's a good answer; you should post it! Except that when I do that, I see a bunch of blank space (looks like about an additional quad space) before (but not after) each inline code snippet. – Quuxplusone Mar 01 '17 at 01:01
  • @Quuxplusone: I don't in your small example. Perhaps you're doing something different? – Werner Mar 01 '17 at 01:02
  • Aha, if I remove \lstset{autodedent} the extra space goes away. But actually I want to get rid of basicstyle=\ttfamily\footnotesize in the inline context as well, or else the text becomes too small when it's inline. Time to read even more of the listings manual, I guess... – Quuxplusone Mar 01 '17 at 01:06

1 Answers1

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 02 '17 at 06: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 02 '17 at 07:02