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?

2
  • 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, 2015 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, 2015 at 20:01

1 Answer 1

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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .