4

I'm explaining some code that uses Greek characters, bold, italic, and accents.

The default font does not differentiate between bold and non-bold.

\documentclass{minimal}
\usepackage[LGR,T1]{fontenc}
\DeclareSymbolFont{ttgreek}{LGR}{cmtt}{m}{n}
\DeclareMathSymbol{\ttdelta}{\mathord}{ttgreek}{`d}
\begin{document}

\texttt{Writing some \textbf{bold}, \emph{italic},
Greek $\ttdelta$, and accents $\mathtt{\tilde{y}}$.}

\end{document}

enter image description here

Using \usepackage[lighttt]{lmodern} lets me distinguish between bold and non-bold, but the italic becomes slanted and the Greek keeps being bold.

enter image description here

What I want is:

  • differentiate between bold and non-bold,
  • have a working italic,
  • use non-bold Greek letters like δ, and
  • keep using accents.

Use case

I have these requirements for the code snippets in my book. Below, you can see the lack of italic for the comments. The alternative was a lack of bold for the Python keywords.

enter image description here

Then, we compute \(\Fi\) and \(\vzchk\) with the following algorithm.
\begin{CodeEnv}
\pydef dict_inference(net, \m\ce, \m\cc):
  \m\vz0 = net.sample_latent()  \mc{# initial guess, all zeros}
  \m\vzchk = compute_\m\vzchk(\m\ce, \m\vz0)
  t2 = net.top2(\m\vzchk)          \mc{# get top-2 (t2) units}
  \m\vzchk2 = compute_\m\vzchk(\m\cc, t2)
  \m\cF = \m\ce(\m\vzchk2)                 \mc{# compute the free energy}
  \pyreturn \m\cF, net.pad(\m\vzchk2)
\end{CodeEnv}
\begin{compactenum}
  \item compute \(\vzchk\) by minimising \(\ce(\vz)\),
  \item zero all but the two most active components of the code \(\vzchk\),
  \item minimise \(\cc(\vz)\) wrt these two components,
  \item evaluate and return \(\Fiy = \ce(\vzchk)\) and \(\vzchk\).
\end{compactenum}
6
  • do you need math accents? $\mathtt{\tilde{y}}$ not \texttt{\~{y}} ? May 17, 2023 at 18:56
  • I have a macro for my vector-y-tilde \vytld which is used in the math environment and I make it monospaced by encapsulating it in a \mathtt{} macro. My major issue is with a working monospace font that supports italic, bold, and non-bold Greek characters.
    – Atcold
    May 17, 2023 at 19:00
  • I would guess there is a very limited choice of monospace italic (not slanted) fonts May 17, 2023 at 20:16
  • are your delta and the character with accent really math, or are you only using math because it looks right and in reality you have text characters? May 18, 2023 at 9:01
  • My equations use Greek and accents. My Python code reflects such use. The LaTeX reproduces and describes the code. Let me integrate a use case in the question, so you better understand what I'm doing. The math in the code is the same math (same macros) I use in my formulas wrapped in a \mathtt{}. So… I'm not sure I've answered your question. Please, let me know.
    – Atcold
    May 18, 2023 at 14:45

3 Answers 3

2

You can use the xfakebold package.

\documentclass{minimal}

\usepackage[LGR,T1]{fontenc}
\DeclareSymbolFont{ttgreek}{LGR}{cmtt}{m}{n}
\DeclareMathSymbol{\ttdelta}{\mathord}{ttgreek}{`d}
\usepackage[bold=0.3]{xfakebold}

\begin{document}
    \texttt{Writing some \setBold\textbf{bold}\unsetBold, \textit{italic}, Greek $\ttdelta$, and accents \(\mathtt{\tilde{y}}\).}
\end{document}

bold textttt

4
  • Someone knowledgeable in macros can change the textbf command in the texttt environment to automatically insert setBold and unsetBold commands automatically, or define a new command which does this. May 18, 2023 at 4:57
  • Honestly, this looks very good and I'd be handling the formatting part with macros, so the \[un]setBold is not even a problem. I just wonder why the overall text looks thinner than the one I showed in my screenshot. Maybe I'm just hallucinating this…
    – Atcold
    May 18, 2023 at 14:24
  • @Atcold Are you on mac os? If yes, then that might be the reason. The displayed text generally looks thicker on mac os compared to Linux or Windows (I am on Linux, Manjaro KDE). See my comment here: tex.stackexchange.com/questions/460517/… May 18, 2023 at 17:17
  • *I am* on Mac. Though, I used TeXShop for the screenshots. I am using your xfakebold suggestion with the lmtt font suggested by David. I don't know what answer I'm supposed to accept now :/
    – Atcold
    May 18, 2023 at 18:32
4

latin modern typewriter seems to have the forms you need

enter image description here

\documentclass{article}
\usepackage[LGR,T1]{fontenc}
\renewcommand\ttdefault{lmtt}
\DeclareSymbolFont{ttgreek}{LGR}{lmtt}{m}{n}
\DeclareMathSymbol{\ttdelta}{\mathord}{ttgreek}{`d}
\begin{document}

\texttt{Writing some \textbf{bold} and \emph{italic} and
Greek $\ttdelta$ and accents $\mathtt{\tilde{y}}$.}

\end{document}
7
  • 1
    Unfortunately, the bold isn't that much different from the regular weight. This was also true of the original cmtt font. For a project where being able to distinguish between the two was essential, at AMS we defined a real lightface version. But that was in Metafont days, and the font is long gone. May 17, 2023 at 21:26
  • Thanks, David. How did you find this font? Meaning, how would a LaTeX user go around finding such a font, given its desired features (as listed in my post)?
    – Atcold
    May 18, 2023 at 14:28
  • @Atcold er... does "I have been using latex for 35 years and knew the font was there" count as an answer? May 18, 2023 at 14:32
  • Hahaha! What I meant is, where would one look for understanding what cmtt is and where to find the lmtt variant? texdoc cmtt returns a rather old package documentation that has no font table.
    – Atcold
    May 18, 2023 at 16:38
  • 1
    give points to newcomers, I can manage without a green tick... May 18, 2023 at 16:42
2

if you are not bound to pdflatex you could use the lualatex and the newcomputer modern fonts. (The bold is not very bold, but you can embolden it, see the bold italic):

\documentclass{article}
\usepackage{fontspec}

\setmonofont{NewCMMono10-Regular.otf}%
 [%
  ItalicFont=NewCMMono10-BookItalic.otf,%
  BoldFont=NewCMMono10-Bold.otf,%
  %BoldFeatures={FakeBold=2}, %bolder?
  BoldItalicFont=NewCMMono10-BoldOblique.otf,%
  BoldItalicFeatures={FakeBold=2}
 ]

\begin{document}

\texttt{Writing some \textbf{bold} and \emph{italic}  and \emph{\textbf{bold italic}}\\
 and Greek δ and accent ỹ}

\end{document}

enter image description here

1
  • Thanks, Ulrike. There is very little difference between bold and non-bold. I think FakeBold may be a good direction, though. And, as Apoorv Potnis points out, this can be achieved with a package that does not require LuaLaTeX.
    – Atcold
    May 18, 2023 at 15:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .