[Tex/LaTex] automatically adjust the vertical alignment of square brackets in text

kerningtypography

Many roman and sans serif fonts, and the default LaTeX fonts in particular, seem to have a thing with square brackets: they descend a long distance below the baseline, probably in order to "catch" descenders. However, in a lot of cases this looks really odd (at least to me), especially when typesetting things like X[10]:

Weird brackets

For that use case it looks a lot better to me (personally) when the square bracket is centered vertically around the midpoint of the capital letter. Except in the tt font where it already looked fine before and which got broken by the change. I can live with the fact that the g now looks slightly out of place, which was also the case before in the tt version.

Nice brackets

Now I ask more out of curiosity than out of need: Is there a document-based way (i.e. other than adjusting the font files) to shift all square brackets in the current scope (or in the whole document) up bit a certain amount (ideally depending on the current font family and size)? In my limited understanding such a thing would probably require a lot of black magic TeX hackery, breaking many things in the process… or maybe it would not. Maybe one of the wise TeX wizards around here knows the answer.


\documentclass{article}
\newcommand{\R}[1]{\raisebox{0.075em}{#1}}
\begin{document}
\noindent
rm: \textrm{X[10g]}\\
sf: \textsf{X[10g]}\\
tt: \texttt{X[10g]}\\
math: $X[10g]$\\

\noindent
rm: \textrm{X\R[10g\R]}\\
sf: \textsf{X\R[10g\R]}\\
tt: \texttt{X\R[10g\R]}\\
math: $X\R[10g\R]$\\
\end{document}

Best Answer

As David points out in the OP's comments, making the brackets active will break the use of many things, such as optional arguments. So this solution splits the difference. It creates the macros \shftON and \shftOFF to turn the vertically shifted brackets on and off by making them \active or not.

When \shftON is invoked, you can get the brackets you want with the [ and ] characters... and when \shftOFF is invoked, everything reverts to the normal way of doing things. In my MWE, I show this by first demonstrating the \shftON bracket appearances, and then, after turning \shftOFF, I show not only the original brackets in use, but also make use of an optional bracketed argument to show that they are not broken by this technique.

\documentclass{article}
\usepackage{graphicx}% ONLY NEEDED FOR \scalebox DEMO
\setbox0=\hbox{0}
\newlength\htz\newlength\dpz
\htz=\ht0\relax
\dpz=\dp0\relax
\let\svlb[
\let\svrb]
\def\shftlb{\setbox0=\hbox{\svlb}\raisebox{%
  \dimexpr-.5\ht0+.5\htz+.5\dp0-.5\dpz}{\svlb}}
\def\shftrb{\setbox0=\hbox{\svrb}\raisebox{%
  \dimexpr-.5\ht0+.5\htz+.5\dp0-.5\dpz}{\svrb}}
\catcode`[=\active
\catcode`]=\active
\def\shftON{%
  \catcode`[=\active
  \catcode`]=\active
  \def[{\shftlb}%
  \def]{\shftrb}%
}
\catcode`[=12
\catcode`]=12
\def\shftOFF{%
  \catcode`[=12
  \catcode`]=12
}
\begin{document}
\shftON
$X [10] g$\par\textsf{X [10] g}\par\texttt{X [10] g}\par\textrm{X [10] g}
\shftOFF
Revert: \textrm{X [10] g} \scalebox{.35}[.75]{Optional Argument used}
\end{document}

enter image description here

Related Question