2

This question is closely related to several other questions here, but I believe it hasn't been asked (or answered) exactly like this.

Is it possible to change the font size in verbatim and Verbatim using a scale parameter?

For example, I know I can do

\setmonofont[Scale=0.8]{Menlo}

to globally change \texttt. And from answers I've seen here I know I can change the font size in verbatim using

\makeatletter
\patchcmd{\@verbatim}
  {\verbatim@font}
  {\verbatim@font\small}
  {}{}
\makeatother

or adjust Verbatim using

\begin{Verbatim}[fontsize=\small]

But suppose I don't want to specify \small, but instead use a scale factor like 0.8. Can this be done? Is there also a way modify it for all Verbatim blocks as well?

I am using LuaLaTeX.

2

You can define a command for setting the font size to a fraction of the current font size.

\documentclass{article}
\usepackage{fontspec,xfp}
\usepackage{fancyvrb}

\setmonofont{Menlo}[Scale=MatchLowercase]

\makeatletter
\newcommand{\scalefont}[1]{%
  \edef\scale@fontsize{\fpeval{#1*\f@size}}%
  \edef\scale@fontbaselineskip{\fpeval{1.2*\scale@fontsize}}%
  \fontsize{\scale@fontsize}{\scale@fontbaselineskip}\selectfont
}
\makeatother

\begin{document}

This is normalsize with \texttt{monospaced text}
\begin{Verbatim}[fontsize=\scalefont{0.8}]
This is scaled 80%
\end{Verbatim}
Again normalsize
\begin{Verbatim}[fontsize=\scalefont{0.5}]
This is scaled 50%
\end{Verbatim}

\end{document}

enter image description here

| improve this answer | |

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.