1

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?

  • 6
    Try \newcommand{\itbf}[1]{\textit{\textbf{#1}}}. – Levy Aug 23 '19 at 16:41
  • What exactly do you mean by aiming to apply italic and bold at the same time? – andrew.paul.acosta Aug 23 '19 at 17:02
  • 1
    @Sveinung Not that usual ;-) It's an apparently common misconception. What would work would be to add the necessary group: \newcommand{\itbf}[1]{{\itshape\bfseries#1}}. The actual italic correction would still be missing, though. – Phelype Oleinik Aug 23 '19 at 17:11
  • 1
    @andrew.paul.acosta -- An example of where a bold italic font is needed is in a biology paper, where a bold section heading contains a species name, which should be set in italic type. – barbara beeton Aug 24 '19 at 2:14
  • 1
    @barbarabeeton: Nonetheless, in a case like the one you describe, you wouldn’t need to define a font command that switches to that combination of series and shape: you would write, in the source file, something like \section{About \emph{Mammalia}}. – GuM Aug 26 '19 at 11:55
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.