[Tex/LaTex] Define a new font command

fontsmacros

I'd like to define a command aiming to apply italic and bold at the same time. I tried use \newcommand{\itbf}{\textit{\textbf{}}}. Is it correct?

Best Answer

LaTeX provides the \DeclareTextFontCommand declaration precisely for this purpose; in your case, you should say, for example,

\DeclareTextFontCommand{\textbfit}{\bfseries\itshape}

That is, in the first argument you specify a name of your choosing for the new command; and in the second argument, you put the declarations needed to obtain the family, series, and shape you want to change to.

With this method, you define commands that behave exactly as the predefined commands \textit, \textbf, etc., in that

  • they are robust, so they can be used without problems in “moving arguments”;

  • they automatically take care of the italic correction at both ends, in a way that is consistent with what the predefined commands do (actually, by means of the same algorithm).

Indeed, the predefined commands \textit, \textbf, etc. are themeselves defined by means of \DeclareTextFontCommand, as you can readily check in the LaTeX sources (e.g., in latex.ltx). Note that, given this fact, the name you choose for your new command should, by consistency, begin with \text....

MWE:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\DeclareTextFontCommand{\textbfit}{\bfseries\itshape}
\newcommand*{\nonRobustCommand}{I'm not robust!}
\DeclareRobustCommand*{\robustCommand}{I'm robust!}



\begin{document}

Normal text and \textbfit{text that is both boldfaced and italicized}.  By way
of comparison, \textbf{this is text that is oly boldfaced} (but not italicized),
and \textit{this is text that is only italicized} (but not boldfaced).

Test for italic correction:
\begin{itemize}
    \item
        bold-italic followed by normal: \textbfit{f}f
    \item
        bold-italic followed by bold: \textbfit{f}\textbf{f}
    \item
        bold-italic followed by italic: \textbfit{f}\textit{f}
\end{itemize}
In the last case, you might argue that the italic correction should 
\textbfit{not} be used, as in {\bfseries\itshape f}{\itshape f}, but that's the 
way \LaTeX\ is programmed!

\typeout{Test for robustness: \textbfit}
\typeout{(Compare with this: \nonRobustCommand}
\typeout{And with this: \robustCommand)}

\end{document}

Addition

I forgot to mention, among the benefits of this approach, that your new command will automatically work in math mode too, if you load the masmath package (actually, it suffices to load the amstext package), as the predefined commands like \textit and \textbf do.

New MWE:

% My standard header for TeX.SX answers:
\documentclass[a4paper]{article} % To avoid confusion, let us explicitly 
                                 % declare the paper format.

\usepackage[T1]{fontenc}         % Not always necessary, but recommended.
% End of standard header.  What follows pertains to the problem at hand.

\usepackage{amstext}

\DeclareTextFontCommand{\textbfit}{\bfseries\itshape}
\newcommand*{\nonRobustCommand}{I'm not robust!}
\DeclareRobustCommand*{\robustCommand}{I'm robust!}



\begin{document}

Normal text and \textbfit{text that is both boldfaced and italicized}.  By way
of comparison, \textbf{this is text that is oly boldfaced} (but not italicized),
and \textit{this is text that is only italicized} (but not boldfaced).

Test for italic correction:
\begin{itemize}
    \item
        bold-italic followed by normal: \textbfit{f}f
    \item
        bold-italic followed by bold: \textbfit{f}\textbf{f}
    \item
        bold-italic followed by italic: \textbfit{f}\textit{f}
\end{itemize}
In the last case, you might argue that the italic correction should 
\textbfit{not} be used, as in {\bfseries\itshape f}{\itshape f}, but that's the 
way \LaTeX\ is programmed!

Compatibility with the \textsf{amsmath} (or \textsf{amstext}) package:
\( T_{\textbfit{bold-italic}} \ne T_{\textit{italic}} \ne T_{\textbf{bold}} \).

\typeout{--------------------------------}
\typeout{Test for robustness: \textbfit}
\typeout{(Compare with this: \nonRobustCommand}
\typeout{And with this: \robustCommand)}
\typeout{--------------------------------}

\end{document}

This time I’m also showing the output:

Output of the second MWE