2

I'm trying to produce an inline code snippet that starts with a ~.

Using onehalfspacing per university requirement and lineskip=-1pt for subjectively better legibility, packing the \lstinline inside a command shortcut destroys the spacing in all future sections.

Why does this occur? How is the command version parsed differently than what the command is supposed to be a shortcut for? I suspect it has something to do with how ~ is parsed inside a command?

Removing the lineskip or placing any character in front of the ~ fixes this issue, but isn't desirable for me.

MWE:

\documentclass{scrreprt}
\usepackage{listings}
\usepackage{blindtext}
\usepackage[onehalfspacing]{setspace}

\lstset{
    lineskip=-1pt,
    literate={~} {$\sim$}{1}
}

\newcommand{\code}[1]{\lstinline{#1}}

\begin{document}

\section{Inline}
\lstinline{~1}\\
\blindtext

\section{No tilde}
\code{FIX ~1}\\
\blindtext

\section{Tilde breaks everything}
\code{~1}\\
\blindtext

\end{document}
1

Not completely sure why, but

\newcommand{\code}{\lstinline}

with no change in the document code fixes the problem.

Well, there are some clues: with your definition, ~ is tokenized before being passed to \lstinline and this is probably the issue.

  • Is there a way to de-tokenize it, so to speak? – Tobias Weiß Nov 28 '18 at 18:50
  • @TobiasWeiß Why should you define \code to take an argument? – egreg Nov 28 '18 at 18:56
  • Your approach works flawlessly in the MWE. New issue: When it's inside a tabular environment, using your approach throws a "Missing } inserted" error. Should I update the MWE or ask that as a new question? – Tobias Weiß Nov 28 '18 at 21:23
  • @TobiasWeiß Use \code|...| (where | stands for any character not in the code you want to typeset). That's a standard problem with \lstinline. – egreg Nov 28 '18 at 21:29
  • Thank you for replying so quickly, I had just found that in an ancient post here. $ seems to work as well. Still - using my first approach with the argument in place worked even in tabulars. Do you suppose there's a workaround to just get a working code snippet everywhere with the same command, or is that just one of LaTeX's "features"? – Tobias Weiß Nov 28 '18 at 21:31

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.