8

I want to set specific font just for listings in XeLaTeX.

\newfontfamily\consolas{Consolas}

\lstdefinelanguage{MyLanguage}{
    basicstyle=\ttfamily,
    .... % my language definition
}
\lstnewenvironment{MyLanguage}[1][]{
    \lstset{language=MyLanguage,#1}
    \consolas  % do not work
}
{}

\begin{document}
\begin{MyLanguage}[caption={....}]
...
\end{MyLanguage}
\end{document}

I know it is possible to set default mono-space font for whole document with

\setmonofont{Consolas}

and it is working but I do not want other mono-space things like URLs in that font.

Maybe if it is possible to set mono-space font to its default and use this

\lstnewenvironment{MyLanguage}[1][]{
    \lstset{language=MyLanguage,#1}
    \setmonofont{Consolas}
}
{\setmonofont{??default??}}
  • 1
    Welcome to TeX.SX! Did you try \lstset{basicstyle=\consolas}? Also basicstyle=\ttfamily should work when you declare \setmonofont{Consolas}. – egreg May 1 '12 at 15:40
  • Wow... I am amazed, basicstyle=\consolas really works. basicstyle=\ttfamily with \setmonofont{Consolas} was affecting other mono-spaced things like URLs which I want with normal font. Can you write your answer as real answer so i can mark it as answer? :) – NightElfik May 1 '12 at 15:59
10

The "correct" way is to use \setmonofont:

\usepackage{fontspec}
\setmonofont{Consolas}

\usepackage{listings}

\lstdefinelanguage{MyLanguage}{ .... } % my language definition
\lstnewenvironment{MyLanguage}[1][]
  {\lstset{language=MyLanguage,basicstyle=\ttfamily,#1}}
  {}

Should you want a specific monospaced font for only your MyLanguage listings, you can do

\newfontfamily{\consolas}{Consolas}

\lstnewenvironment{MyLanguage}[1][]
  {\lstset{language=MyLanguage,basicstyle=\consolas,#1}}
  {}

I'd add also the columns=fullflexible option, but it's a matter of taste.

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.