5

I'm using minted for pretty printing console output and have just noticed that double hyphens, i.e. --, for program options are getting turned in en-hyphens, i.e. . A MWE is:

\documentclass{memoir}

\usepackage{minted}

\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\setmonofont{Courier}

\begin{document}

\begin{minted}{bash}
0-1--2---3
\end{minted}

\end{document}

and I see:

lualatex output

rather than what I'd hope for, a number of individual hyphens.

While making the MWE, I've realised that it's probably to do with an interaction with the fontspec option Ligatures=TeX, but I can't find a reliable way to selectively disable the option—I can do it inside XeLaTeX, but not luaLaTeX. Is it best just to ensure the mono font is loaded without Ligatures=TeX?

Sam Mason
  • 772

1 Answers1

4

Your conjecture is true:

\documentclass{memoir}

\usepackage{minted}

\usepackage{fontspec}

% disable the default 'Ligatures=TeX' option
\defaultfontfeatures{}
\setmonofont{Courier}

\begin{document}

\begin{minted}{bash}
0-1--2---3
\end{minted}

\end{document}

enter image description here

If you want to have ligatures in running text also for the monospaced font, but not in minted, use a new font family. A small hack is necessary for retrieving the correct font family name.

\documentclass{memoir}

\usepackage{minted}

\usepackage{fontspec}

\setmainfont{TeX Gyre Termes}
\setmonofont{Courier}[Ligatures=TeX]

\defaultfontfeatures{}
\newfontfamily\noligsmonofamily[NFSSFamily=noligsmonofamily]{Courier}

\setminted{fontfamily=noligsmonofamily}

\begin{document}

This---has---ligatures

\texttt{This---has---ligatures}

\begin{minted}{bash}
0-1--2---3
\end{minted}

\end{document}

enter image description here

Sam Mason
  • 772
egreg
  • 1,121,712
  • Yes, I know it's true. I really wanted to be able to have Ligatures=TeX everywhere for compatibility. – Sam Mason Jun 17 '15 at 14:00
  • @SamMason Just issue \defaultfontfeatures{} before defining \setmonofont; the fonts already loaded will respect the \defaultfontfeatures options in force at definition time; the option Ligatures=TeX is implicit at startup, with the most recent version of fontspec. – egreg Jun 17 '15 at 14:03
  • I was hoping I could have the option on most of the time and just have it disabled selectively within minted blocks. Maybe I should just create a version of the mono font with TeX ligatures off and just have minted specifically use it? – Sam Mason Jun 17 '15 at 15:09
  • @SamMason That would indeed be the best choice. I'll add some code later. – egreg Jun 17 '15 at 15:23
  • @SamMason Solution on line – egreg Jun 17 '15 at 15:46
  • wow, that's a bit more fiddly than I expected, but thanks! – Sam Mason Jun 17 '15 at 21:32
  • @SamMason I wish fontspec provided a hook for the allocated family name after doing \newfontfamily. Unfortunately, fancyvrb (which manages the key fontfamily) only accepts an explicit family name as value, so a hypothetical fontfamily=\noligsmono would issue errors. – egreg Jun 17 '15 at 21:36
  • I've just had a play, and seen you can do: \newfontfamily\mintedfont{Courier}[NFSSFamily=mintedfamily] \setminted{fontfamily=mintedfamily} – Sam Mason Jun 18 '15 at 09:20
  • @SamMason Interesting; although it's the reverse of what I did. – egreg Jun 18 '15 at 09:24