Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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??}}
share|improve this question
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

1 Answer 1

up vote 8 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.