7

I'm trying to set up tcolorbox blocks using minted engine. I want to use a font of my choice in those, not the default monospaced one (or tt or helvetica, per minted manual). Let's say I want 'Code New Roman'.

In the following MWE the commented out style-setting line works, but requires of me knowing that (internal?) format. The line without \exfont works but sets the document's default font for tcolorbox blocks. If the line with \exfont is set last and so is actually used in listings, listings do not compile.

At the same time, this tex fragment works in the main body of document.

Is it possible to make the line with \exfont work? Or do that auto-adjustment to a specific font in some other way?

\documentclass{article}
\usepackage{fontspec}
\usepackage[all]{tcolorbox}
\newfontfamily\exfont{Code New Roman}

\newtcblisting{exprog}{
  listing only,
  listing engine=minted,.
%  minted options={ fontfamily={CodeNewRoman(0)} }, % minted expects that sort of string
  minted options={ fontfamily={\exfont\csname f@family\endcsname} }, % doesn't work
  minted options={ fontfamily={\csname f@family\endcsname} }, % works but sets the default font for tcolorbox blocks
}

\begin{document}
{\exfont\csname f@family\endcsname} % works
\begin{exprog}
  ts( n : integer ) : function: double;
\end{exprog}
\end{document}
0

1 Answer 1

6

The minted option fontfamily expects a family name and you don't need to guess what fontspec will assign automatically.

\documentclass{article}
\usepackage{fontspec}
\usepackage[minted]{tcolorbox}

\newfontfamily\exfont{Source Code Pro}[
  NFSSFamily=CNR,
  Scale=MatchLowercase,
]

\newtcblisting{exprog}{
  listing only,
  listing engine=minted,
  minted options={fontfamily=CNR}
}

\begin{document}

\texttt{This is in the standard monospaced font}

{\exfont This is in the alternate monospaced font}

\begin{exprog}
  ts( n : integer ) : function: double;
\end{exprog}
\end{document}

I replaced with Source Code Pro the Code New Roman that I don't have. Here CNR is an arbitrary string (still unused as a family name, of course; the risk of conflict is quite small if you use uppercase letters).

output

6
  • 2
    The more general solution for sure. I had never noticed the NFSSFamily key in fontspec. Commented yesterday
  • 1
    @AlanMunn Very useful, actually: tex.stackexchange.com/search?q=NFSSFamily+user%3A4427 Commented yesterday
  • @egreg Thank you very much! Would you point out the source of the issue? The way minted sets up its environments? Or tcolorbox handling of the options? Commented 20 hours ago
  • @yury10578 I'm not sure what issue you're referring to. Commented 15 hours ago
  • @egreg: Sorry, I meant the issue with {\exfont\csname f@family\endcsname} not working in minted options, xelatex telling me something about there being no \endcsname (while this same code works fine in the main body of the document). I've noticed none of the responders have mentioned that. Commented 5 hours ago

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.