1

I am trying to define a macro ro select the font for the code within the lstlisting (i.e., using listings package). I am referring to the answer from this question What is the least invasive way to set the font for listings?

So I am defining the macro as follows:

\makeatletter
\newcommand\BeraMonottfamily{%
  \def\fvm@Scale{0.85}% scales the font down
  \fontfamily{fvm}\selectfont% selects the Bera Mono font
}
\makeatother

So when I am using code like this, it works fine:

\begin{lstlisting}[basicstyle=\BeraMonottfamily]
...
\end{lstlisting}

However, I don't understand how to use the @Scale{0.85} in the macro. When I am changing the number inside the @Scale{}, nothing is changed. I thought it will change the font size but it does not.

Therefore, what does @Scale{0.85} mean? How to use it? Thank you.

1 Answers1

2

You're using the single macro \fvm@Scale: since \def\fvm@Scale enters TeX's memory when \makeatletter is in force, \fvm@Scale is a single token.

Why do you need it? Because the contents of t1fvm.fd is

\ProvidesFile{t1fvm.fd}[2004/09/07 scalable font definitions for T1/fvm.]

\expandafter\ifx\csname fvm@Scale\endcsname\relax \let\fvm@@Scale@empty \else \edef\fvm@@Scale{s*[\csname fvm@Scale\endcsname]}% \fi

\DeclareFontFamily{T1}{fvm}{\hyphenchar \font -1}

\DeclareFontShape{T1}{fvm}{m}{n}{ <-> \fvm@@Scale fvmr8t }{}

(similar lines omitted), and you seem to want to scale down the font to 85%.

So when \BeraMonottfamily is executed, the scaling factor is recognized. This would be needed just the first time, but it doesn't hurt if done several times.

On the other hand, this font definition file is supposed to be loaded via \usepackage[beramono} that, like other similar packages, offers an option for scaling the font.

I believe you should do this, rather than the indirect method: it makes little sense to use different monospaced fonts across the document.

\documentclass{article}
\usepackage[T1]{fontenc} % needed for Bera Mono
\usepackage[scaled=0.85]{beramono}

\usepackage{listings}

\lstset{basicstyle=\ttfamily}

\begin{document}

Some text and \texttt{monospaced Bera Mono} and some other text

\begin{lstlisting} This uses Bera Mono scaled 85% \end{lstlisting}

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thank you for your answer. I understand your method. However, my question is that when I am defining and using the macro as mentioned above, the scale factor inside the macro is not working. I want to know why and how to use the scale factor inside the macro. – liaoming999 Nov 13 '23 at 23:44
  • Specifically, in my macro, \def\fvm@Scale{0.85} is the same as, let's say, \def\fvm@Scale{0.15}. The font size doesn't change at all. – liaoming999 Nov 13 '23 at 23:49
  • @liaoming999 Did you add \usepackage[T1]{fontenc}? – egreg Nov 14 '23 at 07:56
  • OK thank you. I understand now. It's not about the scale factor. It's about the I am defining the macro in another font and it's not working. – liaoming999 Nov 14 '23 at 10:09
  • @liaoming999 Not really: Bera Mono is only available in T1 encoding and if you don't enable it, you get a completely different font. – egreg Nov 14 '23 at 13:41