2

I am trying to define an inline \code command (not language specific, just a smarter \texttt{} without the need to escape underscores or tildes, for example). I tried the three versions below based on listings. The best looking would be \codeThree, but it suddenly fails when being used in an enumerate environment. Can this be fixed?

\documentclass{scrartcl}

\usepackage{listings}
\newcommand*{\codeOne}[1]{\lstinline[basicstyle=\ttfamily]{#1}}
\newcommand*{\codeTwo}[1]{\scantokens{\lstinline[basicstyle=\ttfamily]{#1}}}
\newcommand*{\codeThree}[1]{\lstinline[basicstyle=\ttfamily, literate={~}{{$\sim$}}1 ]{#1}}

\begin{document}
% \codeOne{~/.bashrc} % fails to display the tilde; don't comment in (side-effects on the below)
\lstinline[basicstyle=\ttfamily]{~/.bashrc} % works
\codeTwo{~/.bashrc} % also works (but only due to the \scantokens command; see https://tex.stackexchange.com/questions/175919/pathological-listings-problem-tilde-in-lstinline-in-a-footnote )
\codeThree{~/.bashrc} % works (and shows nicest tilde)
\begin{enumerate}
\item ... but not in an environment (see \codeThree{~/.bashrc}). % fails
\end{enumerate}
\end{document}

Alternatively, one could work with \verb, but defining a command \code which (adjusts the font size and) passes its argument to \verb seems pretty hopeless (?) I guess (see https://texfaq.org/FAQ-verbwithin )

2

Just don't define \code to take an argument.

\documentclass{scrartcl}

\usepackage{listings}

\newcommand*{\code}{\lstinline[basicstyle=\ttfamily,literate={~}{{$\sim$}}1]}

\begin{document}

\lstinline[basicstyle=\ttfamily]{~/.bashrc}        

\lstinline[basicstyle=\ttfamily, literate={~}{{$\sim$}}1]{~/.bashrc}        

\code{~/.bashrc}

\begin{enumerate}
\item ... also in an environment (see \code{~/.bashrc}).        
\end{enumerate}

\end{document}

enter image description here

You also have the advantage you can call also \code|...| as needed in certain cases.

  • ... that is awesome, thanks a lot! Given its simplicity, I have to mark this as the preferred answer. – Marius Hofert May 25 at 11:10
2

\lstinline is also a verbatim command, you can't put it simply in the body of another command (and also outside the enumerate you get messages about open simple groups in the log). Copy the \lstinline definition instead:

\documentclass{scrartcl}

\usepackage{listings}
\makeatletter
\newcommand\codeThree[1][]{%
    \leavevmode\bgroup % \hbox\bgroup --> \bgroup
      \def\lst@boxpos{b}%
      \lsthk@PreSet
     \lstset{flexiblecolumns,basicstyle=\ttfamily,literate={~}{{$\sim$}}1,#1}%
      \lsthk@TextStyle
      \@ifnextchar\bgroup{%
        \afterassignment\lst@InlineG \let\@let@token}%
                         \lstinline@}
 \makeatother                        
\begin{document}
\lstinline[basicstyle=\ttfamily]{~/.bashrc} % works

\codeThree{~/.bashrc} % works (and shows nicest tilde)
\begin{enumerate}
\item ... and in an environment (see \codeThree{~/.bashrc}). % fails
\end{enumerate}
\end{document}

enter image description here

  • Hi Ulrike, thanks a lot. With listings (first word) you probably mean \lstinline. Interesting, seems like a bug in the listings package then (not to define it the way you did). – Marius Hofert May 24 at 20:59
  • I edited the answer. And it is not a bug, Iistings does define \lstinline this way, I copied the code from the package. – Ulrike Fischer May 24 at 21:08
  • ah, I now understand the solution better, thanks a ton! – Marius Hofert May 24 at 21:33

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.