5

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:

output obtained from the test case

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} – egreg Nov 16 '17 at 15:59
  • 1
    or \usepackage{graphicx} and \scalebox{0.75}{\BUseVerbatim{AlignTest}} (mind the B(oxed) command). – Marijn Nov 16 '17 at 16:08
  • @egreg \usepackage{lmodern} reduces the alignment issue, but doesn't solve it completely. – vinc17 Nov 16 '17 at 17:00
4

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}

enter image description here

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\font`0]{...}

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? – vinc17 Nov 17 '17 at 0:32
  • @vinc17 Generally, in monospaced fonts the em is twice the width of the letters. – egreg Nov 17 '17 at 8:36
  • You could add the more general approach of \widthof or something like that? – Manuel Nov 17 '17 at 9:27
  • 1
    @Manuel \fontcharwd\font`0 is easier than \widthof. – egreg Nov 17 '17 at 9:30

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.