0

I'm using the code presented by this answer (which itself is based on this) to suppress line numbers on some lines in lstlisting. This works, but after I use it, the spacing in lstinline is messed up:

messed up lstinline spacing

This seems to be because of |\Reactivatenumber{12}|. Why is that and how do I fix it?

MWE:

\documentclass{article}
\usepackage{listings}
\lstset{numbers=left,escapeinside=||, basicstyle=\ttfamily\footnotesize}

\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
     \advance\c@lstnumber-\@ne\relax%
    }%
}

\newcommand*\Reactivatenumber[1]{%
  \setcounter{lstnumber}{\numexpr#1-1\relax}
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
   \refstepcounter{lstnumber}
  }%
}
\makeatother

\begin{document}
This looks fine: \lstinline!some-words-with-minus! more text\dots
\begin{lstlisting}[firstnumber=1]
var myFunc = function() {|\Suppressnumber|
    //my function does many great things
    //and it's only 10 lines long!|\Reactivatenumber{12}|
}
\end{lstlisting}

This looks awful: \lstinline!some-words-with-minus! some more text
\end{document}
0

OK found it: I needed to add a % after \refstepcounter{lstnumber}.

Complete snippet:

\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
     \advance\c@lstnumber-\@ne\relax%
    }%
}

\newcommand*\Reactivatenumber[1]{%
  \setcounter{lstnumber}{\numexpr#1-1\relax}
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
   \refstepcounter{lstnumber}%
  }%
}
\makeatother

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.