9

When using babel 'frenchb' with xelatex, spaces are introduced before colons and other characters. This is great for prose, but this is also being applied to code snippets. This yields an unacceptable result when rendering code as a monospaced font.

The image below is supposed to read "a:b:c" but the colons are hidden under other letters because of the extra spacing:

enter image description here

It was generated with this code:

\documentclass{report}
\usepackage{listings} 
\usepackage[frenchb]{babel} 

\lstset{basicstyle=\ttfamily, columns=fixed}

\begin{document}

\begin{lstlisting}
a:b:c
\end{lstlisting}

\end{document}

I'm not sure how I should remedy this. I've already tried using other fonts to no avail, and setting columns=flexible has the effect of wrecking the vertical alignment because of the extra spaces.

3 Answers3

6

The fix was to add \NoAutoSpacing to the basicstyle property of lstset.

\documentclass{report}
\usepackage{listings} 
\usepackage[frenchb]{babel} 

\lstset{ basicstyle=\ttfamily\NoAutoSpacing, columns=fixed }

\begin{document}

\begin{lstlisting}
a:b:c
\end{lstlisting}

\end{document}

\NoAutoSpacing is documented here. It is a feature found in frenchb.dtx, which defines all the language definition macros for the French language.

What remains a mystery to me is why this is needed with xelatex but not with straight latex.

  • Note that this way, you might need to add this command to any style you use in your listings. – T. Verron Jun 05 '13 at 15:28
  • 1
    The newest version of frenchb uses with xelatex \XeTeXinterchartoks to add spaces before a colon. It doesn't make the colon active in xelatex as would be done in latex. And so listings can't make it inactive. – Ulrike Fischer Jun 05 '13 at 16:43
  • I really prefer this solution. Way cleaner, as this does not require to play with esoteric etoolbox stuffs proposed in the other answer. And AFAIK if we use XeTeXinterchartoks we might have all spaces before colons removed even outside the listings environment. But on my side, replacing \NoAutoSpacing by \XeTeXinterchartoks does not work :-/ – wget Nov 23 '15 at 23:02
6

Add the relevant code in front of \lstlisting, which is executed in a group when \begin{lstlisting} is encountered.

\documentclass{report}

%Uncomment the following to try Polyglossia (and comment the babel line)
%\usepackage{polyglossia}
%\setmainlanguage{french}

\usepackage[frenchb]{babel}

\usepackage{listings}
\lstset{basicstyle=\ttfamily, columns=fixed}

\usepackage{etoolbox}
\makeatletter
\@ifundefined{XeTeXinterchartokenstate}
 {\pretocmd{\lstlisting}{\NoAutoSpacing}{}{}}
 {\pretocmd{\lstlisting}{\XeTeXinterchartokenstate=0 }{}{}}
\makeatother

\begin{document}

\begin{lstlisting}
a:b:c
\end{lstlisting}

\end{document}

This should be compatible with the new release of Babel 3.9 that uses \XeTeXinterchartokenstate when XeLaTeX is used (Polyglossia already does it). I tested it on the prerelease version of TeX Live 2013.

egreg
  • 1,121,712
1

Babel with French option makes : an active character. You can locally disable this behaviour with \shorthandoff{:} and re-enable it with \shorthandon{:}.

This would give the following (untested) code

\documentclass{report}
\usepackage{fontspec,listings} 
\usepackage[french]{babel} 

\lstset{basicstyle=\ttfamily, columns=fixed}

\begin{document}

\shorthandoff{:}
\begin{lstlisting}
a:b:c
\end{lstlisting}
\shorthandon{:}

\end{document}

I based this answer on this related question.

M. Toya
  • 3,227
  • 1
    This isn't working. As far as I know, the listings environment should disable the shorthands itself, which it does with pdflatex. – T. Verron Jun 05 '13 at 15:10
  • 1
    This isn't working right off the bat, but when compiling this, xelatex invited me to use \NoAutoSpacing "inside a group". Apparently it's a feature from frenchb.dtx. I'll try to figure out how to include it automatically in listings. – spacecolon Jun 05 '13 at 15:16
  • \shorthandon doesn't work with XeLaTeX, which is a shame because it is often convenient to compile with XeLaTeX when typing in foreign languages like French. – PatrickT Apr 27 '16 at 20:37