5

I wanted to raise the _ char in code listings because the T1 font I am using places it too much below the baseline; this causes identifiers like bla_bla look horrible. I am using listings so I thought I could use the literate option to search/replace _ with a raised version. Unfortunately, the search/replace option conflicts with the lexer which will break the identifiers when finding _.

A MWE to illustrate the issue:

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{libertine}

\usepackage{listings}

\lstset{
  columns=[l]flexible,
  basicstyle={\sffamily},
  morekeywords={to},
}

\begin{document}

\begin{lstlisting}
add_to_list(x)
\end{lstlisting}

\begin{lstlisting}[
  literate={
      {_}{{\raisebox{.3ex}{\_}}}1
  }]
add_to_list(x)
\end{lstlisting}

\end{document}

preview

Is there a way to get this behaviour without disrupting the lexer?

  • I don't know the answer to this, but I think it probably lies closer to listings' SelectCharTable and lst@DefSaveDef than to literate. – jub0bs Mar 7 '15 at 19:56
  • @Jubobs thanks for looking into this. Answers that do not meddle with listings internals/options but with the fonts themselves are also acceptable. – Bordaigorl Mar 7 '15 at 20:01
5

enter image description here

\documentclass{article}

\usepackage[T1]{fontenc}
\usepackage{libertine}

\usepackage{listings}

\lstset{
  columns=[l]flexible,
  basicstyle={\sffamily},
  morekeywords={to},
}

\begin{document}
\begin{lstlisting}
add_to_list(x) 
\end{lstlisting}

\makeatletter
\lst@CCPutMacro
   \lst@ProcessLetter{"5F}{{\raisebox{0.3ex}{\_}}}\@empty\z@\@empty
\makeatother

\begin{lstlisting}
add_to_list(x) 
\end{lstlisting}

\end{document}

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.