# Creating custom font commands which behave like LaTeX size commands

I am writing a document class, which has a few custom text elements. The text elements have concrete requirements regarding color, font family, size, leading etc. I have created a few custom commands and I would like them to behave like \small, \large etc. My current approach is

\NewDocumentCommand { \MarginFont } { }
{ \color{gray}\normalfont\fontsize{8pt}{10.4pt}\selectfont }


However, this does not always behave as expected, e.q. sometimes an extra linebreak is inserted as in the following MWE

\documentclass{article}

\usepackage[margin=2cm]{geometry}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{blindtext}

\NewDocumentCommand { \MarginFont } { }
{ \color{gray}\normalfont\fontsize{8pt}{10.4pt}\selectfont }

\setlength{\parindent}{0pt}

\begin{document}

\begin{minipage}[t]{0.5\linewidth}
\blindtext
\end{minipage}\hfill%
\begin{minipage}[t]{0.45\linewidth}
{\MarginFont \blindtext\par}
\end{minipage}
\vspace*{2cm}

\begin{minipage}[t]{0.5\linewidth}
\blindtext
\end{minipage}\hfill%
\begin{minipage}[t]{0.45\linewidth}
{\small \blindtext\par}
\end{minipage}

\end{document}


The minipage with the \MarginFont is for some reason not correcty aligned. This does not happen when I use any LaTeX size command such as \small.

In the Document Classes Documentation, I found the much more sophisticated code for the definition of \small.

\DeclareRobustCommand\small{%
⟨*10pt⟩
\@setfontsize\small\@ixpt{11}%
\abovedisplayskip 8.5\p@ \@plus3\p@ \@minus4\p@
\abovedisplayshortskip \z@ \@plus2\p@
\belowdisplayshortskip 4\p@ \@plus2\p@ \@minus2\p@
\def\@listi{\leftmargin\leftmargini
\topsep 4\p@ \@plus2\p@ \@minus2\p@
\parsep 2\p@ \@plus\p@ \@minus\p@
\itemsep \parsep}%
⟨/10pt⟩


How can I change my definition to behave the same? Also, will TeX loose is ability to produce well formatted paragraphs, if I set a fixed baselineskip instead of the various definitions with plus and minus. If so, how can I add this behaviour to my custom font commands?

Bonus question: Currently, the user has to add a \par before closing the group in order for the baselineskip to take effect. Is there a smarter way to define the commands such that this is no longer required? I know, that with the minipage the group braces are not needed, but these commands should also be used inside normal text.

Edit: I know that adding \vspace{0pt} can fix the alignment, but I am interested what caused it in the first place.

• texdoc grfguide footnote page 6 Jan 5 at 11:47
• \vspace{0pt} wouldn't really fix the alignment, it just makes it wrong on both sides so the error is less apparent. Jan 5 at 11:48
• the alignment issue is unrelated to the font change it is just an effect of \color Jan 5 at 11:50
• @DavidCarlisle thanks, I also noticed that sometime \vspace{0pt} inserts to much vertical space if the minipages are enclosed in other text.
– marv
Jan 5 at 11:51
• it adds 0pt of space but (like \color) it moves the reference point of the minipage to the top edge (above the capital L) rather than the first baseline (the base of the capital L) see the image you posted for the \small example if you added vspace0pt to both sides the small text would all move up so that the top of the \small L aligned with the top of the normalsize L Jan 5 at 11:54

You are adding a space token before \color which can produce extra horizontal space, but also you want \leavevmode to ensure the paragraph starts before the \color whatsit, otherwise (as here) the color whatsit will be on its own line above the text.

\documentclass{article}

\usepackage[margin=2cm]{geometry}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{blindtext}

\NewDocumentCommand \MarginFont  { }
{\leavevmode\color{gray}\normalfont\fontsize{8pt}{10.4pt}\selectfont }

\setlength{\parindent}{0pt}

\begin{document}

\begin{minipage}[t]{0.5\linewidth}
\blindtext
\end{minipage}\hfill%
\begin{minipage}[t]{0.45\linewidth}
\MarginFont \blindtext
\end{minipage}
\vspace*{2cm}

\begin{minipage}[t]{0.5\linewidth}
\blindtext
\end{minipage}\hfill%
\begin{minipage}[t]{0.45\linewidth}
\small \blindtext
\end{minipage}

\end{document}


Note I deleted the unneeded brace groups so \par is not needed at the end of the text.

• This works! My \par question was a little unclear, but the commands are not only intended to be used inside minipage. Therefore I can't always remove the brace groups. I have edited the question.
– marv
Jan 5 at 11:57
• Also, can you tell me something regarding the TeX spacing question with plus and minus. Is there a better way to define spacing than \fontsize.
– marv
Jan 5 at 11:58
• plus and minus are for stretchy lengths which you (almost) never want to use for \baselineskip so \fontsize is fine although really it might make more sense to simply use \footnotesize (which is also 8pt) and change footnotesize to use 10.4 rather than its default 9.5 baselineskip. The idea of the named sizes is to centralize choice of explicit lengths not to have them embedded in your code as here. @marv Jan 5 at 12:03
• @marv you never normally use \par in a document, if the text set at this font size is multi-paragraph you just end each paragraph with a blank line as normal. if it is delimited by an enviornment that usually ends the paragraph. If it is a single sentence delimited with {} set within a larger paragraph then you want the baseline skip of ther outer paragraph to apply in any case so there is no real issue to solve there. Jan 5 at 12:06
• ok, this is a problem of only providing a MWE. I have quite a few custom sizes and most of them don't coincide with a predefined size as \footnotesize. My misconceptions was, that TeX relies on having flexible size for baselineskip. Thanks for clarifying!
– marv
Jan 5 at 12:07