Creating custom font commands which behave like LaTeX size commands

fontsfontsize

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}

minipages not aligned

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.

Best Answer

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.

enter image description here

\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.

Related Question