7

How can I force the spacing of a character?
I tried using a zero-width box, but its content gets shifted to the left (not sure why).

\documentclass{minimal}\usepackage{fancyvrb}\textwidth=9cm
\begin{document}
\begin{Verbatim}[commandchars=\\\{\}, codes={\catcode`$=3\catcode`^=7}]
L = F(x, y)         % align to this
$\mathcal{L}$ = F(x, y).mean()  % $\mathcal{L}$ takes too much space
\makebox[0pt]{$\mathcal{L}$}  = F(x, y).mean()  % the zero box shifts left
\end{Verbatim}
\end{document}

enter image description here

2
  • 1
    I assume this example is just a toy example? Otherwise something like the align* environment should allow you to align at the equals sign. No need to fiddle with character widths manually...
    – Raven
    Nov 15, 2022 at 18:29
  • 2
    Yeah. I use this environment to illustrate my code. Sometimes I need to use a mathematical symbol which messes up the spacing.
    – Atcold
    Nov 15, 2022 at 18:32

2 Answers 2

6

You want a left aligned makebox (not centred)

enter image description here

\documentclass{minimal}\usepackage{fancyvrb}\textwidth=9cm
\begin{document}
\begin{Verbatim}[commandchars=\\\{\}, codes={\catcode`$=3\catcode`^=7}]
L = F(x, y)         % align to this
\makebox[0pt][l]{$\mathcal{L}$}  = F(x, y).mean()  % the zero box shifts left
\end{Verbatim}
\end{document}
1
  • Dang, I should read more carefully… «The […] optional argument pos specifies how the text is positioned within the box. With no value given, the text is centred.» \cite{KopkaDaly}. Apologies… I assumed it was left aligned by default.
    – Atcold
    Nov 15, 2022 at 18:36
5

I'd prefer centering in the space for letters (0.5em), but I also propose horizontally scaling the symbol.

\documentclass{article}
\usepackage{fancyvrb}
\usepackage{graphicx}

\makeatletter
\newcommand{\tcal}[1]{%
  \sbox0{$\m@th\mathcal{#1}$}%
  \scalebox{\fpeval{(0.5em)/(\wd0)}}[1]{\usebox0}%
}
\makeatother
\newcommand{\lcal}[1]{\makebox[0.5em][l]{$\mathcal{#1}$}}
\newcommand{\ccal}[1]{\makebox[0.5em]{$\mathcal{#1}$}}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\}]
L = F(x, y)
\tcal{L} = F(x, y).mean()
\lcal{L} = F(x, y).mean()
\ccal{L} = F(x, y).mean()
\tcal{L}(x)+\tcal{Q}+x
\lcal{L}(x)+\lcal{Q}+y
\ccal{L}(x)+\ccal{Q}+z
\end{Verbatim}

\end{document}

Compare the outputs and decide.

enter image description here

1
  • Wow, impressive. Indeed \tcal has to be chosen when combining multiple symbols, while \ccal would be the go-to for when spacing is added. Thanks a bunch, this is really a remarkable answer, @egreg.
    – Atcold
    Nov 20, 2022 at 23:32

You must log in to answer this question.

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