如何取消括号内的粗体和斜体文本?

如何取消括号内的粗体和斜体文本?

是否有命令可以使括号内的文本无效、覆盖或取消任何字体样式?

就像是:

\nullifystyle{sample text: \textit{this should be not italic} and \textbf{this should be not bold}}

\normalfont\textnormal\textup\upshape完全相反的事情。参见:如何设置不斜体或者不粗体?

用例:我需要它从文档的其他部分调用文本片段,但要覆盖字体样式。MWE 将是:

\documentclass{article}

\begin{document}

\newcommand{\nullifystyle}{???} %is there already a command for this? or how to define it?

\newcommand{\textsample} {some normal text, \textit{some italic} and \textbf{some bold}}

Here I would like to write \textsample. \par

Here  \nullifystyle{\textsample} \ should be typed  totally with normal font style.


\end{document}

答案1

局部重新定义\selectfont不尊重形状和系列的变化:

\documentclass{article}

\makeatletter
\newcommand{\nullifystyle}[1]{%
  \begingroup
  \edef\sv@f@shape{\f@shape}%
  \edef\sv@f@series{\f@series}%
  \let\sv@selectfont\selectfont
  \def\selectfont{%
    \fontshape{\sv@f@shape}%
    \fontseries{\sv@f@series}%
    \sv@selectfont
  }%
  #1%
  \endgroup
}
\makeatother

\begin{document}

\nullifystyle{sample \emph{text}: \textit{this should be not italic}
  and \textbf{this should be not bold}\textsuperscript{test}}

\end{document}

在此处输入图片描述

如果你使用像示例中那样的宏,它的工作方式是一样的

\newcommand{\sampletext}{sample \emph{text}: \textit{this should be not italic}
  and \textbf{this should be not bold}\textsuperscript{test}}

\nullifystyle{\sampletext}

答案2

您可以重新定义需要取消的命令,如下所示。

\documentclass{article}

\newcommand\noop[1]{#1}
\newcommand\nullifystyle[1]{\begingroup\let\textit\noop\let\textbf\noop
  #1\endgroup}

\begin{document}
\textit{This should be italic} and \textbf{this should be bold}.

\nullifystyle{\textit{This should not be italic} and \textbf{this
    should not be bold}}.

\textit{This should be italic} and \textbf{this should be bold}.
\end{document}

但我认为最好解释一下您需要它做什么,然后可能会提出更好的解决方案。

相关内容