添加

添加

我想定义一个命令,旨在同时应用斜体和粗体。我尝试使用\newcommand{\itbf}{\textit{\textbf{}}}。这样正确吗?

答案1

LaTeX\DeclareTextFontCommand正是为此目的而提供的声明;对于你的情况,你应该说,例如,

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

也就是说,在第一个参数中,您可以为新命令指定您选择的名称;在第二个参数中,您可以输入获取要更改为的系列、系列和形状所需的声明。

使用此方法,您可以定义行为的命令确切地作为预定义命令\textit\textbf等,其中

  • 它们非常强大,因此可以在“移动论证”中毫无问题地使用;

  • 它们会自动处理两端的斜体校正,方式与预定义命令一致(实际上,通过相同的算法)。

事实上,预定义的命令\textit\textbf本身通过 定义\DeclareTextFontCommand,你可以在 LaTeX 源中轻松检查(例如,在 中latex.ltx)。请注意,鉴于这一事实,您为新命令选择的名称应始终以 开头\text...

梅威瑟:

% 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}

添加

我忘了提到,这种方法的优点之一是,如果您加载包masmath(实际上,加载amstext包就足够了),您的新命令也将自动在数学模式下工作,就像预定义命令\textit一样\textbf

新 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}

这次我还展示了输出:

第二个 MWE 的输出

相关内容