# Typewriter font for math symbols (∃, ∀) in scaled verbatim environment for pdflatex

I would like to use math symbols (such as ∀ and ∃) in a scaled verbatim environment, using a typewriter font. The current answer to Proper typewriter font for math symbols (∀, ∃, →), e.g. in verbatim environment does not work for a scaled verbatim environment with \scalefont from the scalefnt package. And I also want to stick to pdflatex (not xetex).

Here's a test case based on this answer:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pmboxdraw}
\usepackage{newunicodechar}
\usepackage{fancyvrb}
\usepackage{scalefnt}

\DeclareFontFamily{U}{cmtex}{}
\DeclareFontShape{U}{cmtex}{m}{n}{
<-> cmtex10
}{}
\newcommand{\cmtex}[1]{{%
\usefont{U}{cmtex}{m}{n}\symbol{#1}%
}}

\newcommand{\ttexists}{\cmtex{"15}}
\newcommand{\ttforall}{\cmtex{"14}}

\newunicodechar{∃}{\ifmmode\exists\else\ttexists\fi}
\newunicodechar{∀}{\ifmmode\forall\else\ttforall\fi}

\begin{SaveVerbatim}{AlignTest}
│∃∀∃∀∃∀∃∀∃∀∃∀∃∀∃∀│
│1234567812345678│
\end{SaveVerbatim}

\begin{document}

\UseVerbatim{AlignTest}

{\scalefont{0.75}
\UseVerbatim{AlignTest}
}

\end{document}


When compiling this, I get warnings, such as:

LaTeX Font Warning: Font shape T1/cmr/m/n' in size <7.5> not available
(Font)              size <7> substituted on input line 32.


and here's what I get:

showing a bad alignment only when the font is scaled. Using a \makebox can solve the alignment issue:

\newcommand{\ttexists}{\makebox[0pt][l]{\cmtex{"15}} }
\newcommand{\ttforall}{\makebox[0pt][l]{\cmtex{"14}} }


but this can mean that the characters are closer to each other than expected as they don't have the right size in the first place.

How can one properly solve the font size issue (and the alignment issue as a consequence)?

• \usepackage{lmodern} Nov 16, 2017 at 15:59
• or \usepackage{graphicx} and \scalebox{0.75}{\BUseVerbatim{AlignTest}} (mind the B(oxed) command). Nov 16, 2017 at 16:08
• @egreg \usepackage{lmodern} reduces the alignment issue, but doesn't solve it completely. Nov 16, 2017 at 17:00

You can solve the main issue with the fix-cm package, that makes the fonts fully scalable. However the optical scaling used by the standard typewriter font has the consequence that the fixed scaling of cmtex results in slightly narrower characters. It's not difficult to make the characters as wide as those in cmtt by boxing them.

\RequirePackage{fix-cm}
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pmboxdraw}
\usepackage{newunicodechar}
\usepackage{fancyvrb}
\usepackage{scalefnt}

\DeclareFontFamily{U}{cmtex}{}
\DeclareFontShape{U}{cmtex}{m}{n}{
<-> cmtex10
}{}
\newcommand{\cmtex}[1]{{%
\ttfamily\makebox[0.5em]{%
\usefont{U}{cmtex}{m}{n}\symbol{#1}%
}%
}}

\newcommand{\ttexists}{\cmtex{"15}}
\newcommand{\ttforall}{\cmtex{"14}}

\newunicodechar{∃}{\ifmmode\exists\else\ttexists\fi}
\newunicodechar{∀}{\ifmmode\forall\else\ttforall\fi}

\begin{SaveVerbatim}{AlignTest}
│∃∀∃∀∃∀∃∀∃∀∃∀∃∀∃∀│
│1234567812345678│
\end{SaveVerbatim}

\begin{document}

\UseVerbatim{AlignTest}

{\scalefont{0.75}%
\UseVerbatim{AlignTest}
}

\end{document}


In Computer Modern Mono, the em is twice the letter width, so I first switch to it and use the em for the box width.

In case you use \cmtex with other monospaced fonts, you can, for safety, do

\makebox[\fontcharwd\font0]{...}


that would use exactly the width of a digit/letter in the monospaced font.

• Thanks. I confirm that this works. But could you explain the choice of the 0.5em? Is this a property of the verbatim environment? Nov 17, 2017 at 0:32
• @vinc17 Generally, in monospaced fonts the em is twice the width of the letters. Nov 17, 2017 at 8:36
• You could add the more general approach of \widthof or something like that? Nov 17, 2017 at 9:27
• @Manuel \fontcharwd\font0 is easier than \widthof`. Nov 17, 2017 at 9:30