I need to use the \textvisiblespace
character and the Mac Command splat (⌘) in code listings, with the lstlisting
environment and with \lstinputlisting
from an external file. I'm using the \cmd
macro from the menukeys
package to get the splat.
With the MWE below, it works in pdflatex but in XeLaTeX it ignores the literate=
parameter of \lstset
and tries to find both ⌘ and ␣ in the font.
What am I doing wrong?
\documentclass{article}
%%%%%%%%%%%%%%%%%%%%%%%%% either these two lines:
%\usepackage{fontspec}
%\usepackage{libertine} % or any other font
%%%%%%%%%%%%%%%%%%%%%%%%% or these two lines:
\usepackage[utf8x]{inputenc}
\usepackage[T1]{fontenc}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{listings}
\usepackage{menukeys}
\lstset{extendedchars=true,
literate={⌘}{{\cmd}}1{␣}{{\textvisiblespace}}1,
showstringspaces=false,
columns=fullflexible,
keepspaces=true}
\lstloadlanguages{XML,[LaTeX]TeX}
\begin{document}
Here is a listing:
\begin{lstlisting}[language=TeX]
Mac␣users press ⌘-C
\end{lstlisting}
Here is a listing from a file:
\lstinputlisting{foobar.sh}
\end{document}
External file foobar.sh says:
Mac␣users press ⌘-C
In pdflatex, it gets the characters right but issues the error:
! Package tikz Error: Sorry, some package has redefined the meaning of the math
-mode dollar sign. This is incompatible with tikz and its calc library
and might cause unrecoverable errors.
See the tikz package documentation for explanation.
Type H <return> for immediate help.
...
l.18 Mac␣users press ⌘
-C
In XeLaTeX (using the Libertine font) it misplaces the visible space (either to the left of the line in the environment version, or on the line above the code in the version input from the file), and it prints the "unknown character" (square with an X) for the splat. Using the plex-serif font package, I get a thick vertical line for both characters and the error message in the log file (repeated for the splat):
Missing character: There is no ␣ in font [IBMPlexSerif-Regular.otf]/OT:script=l
atn;language=DFLT;mapping=tex-text;mapping=tex-text;!
Omitting the font (ie using CM), I get a similar result to Libertine, but with no character at all for the splat.
listings
a pain with non-ascii characters. Have a look at my answer here: tex.stackexchange.com/a/389822/87678 I can give a full answer that works if you need.