我在文本的某些部分遇到了字体意外变化的情况。这是因为我没有意识到\begin{center}
and\end{center}
环境在字体方面有自己的范围。但是,我仍然不明白为什么在编译以下 MNWE 后会得到以下渲染输出。
\documentclass{article}
\newcommand{\myhead}[1]{\noindent{\large\bf #1}\vspace{2mm}}
\begin{document}
\sffamily%%%Change to sans-serif font
\title{\textbf{This is a sans-serif title...}}%%%as expected
\author{\textbf{...and author...}}%%%as expected
\date{\textbf{...and date.}}%%%as expected
\clearpage\maketitle
\begin{center}%%%note that centering has its own scope regarding fonts.
\ttfamily%%%typewriter within scope
This is an url here.%%%as expected
\normalfont%%%normalfont within scope
\normalsize%%%normalfont within scope
\noindent This is normal text here, as expected.%%%as expected
\end{center}
\myhead{This heading is written in normal text, what you would intuitively expect. However, your intuition is wrong...}
%%%At first you would think that \normalfont above changes the font back to normal. But this is wrong, since it is
%%%within a \begin{center}\end{center} scope. So what you would expect that this text here will be sans-serif.
%%%But it isn't!
...since this text is still written with a sans-serif font.%%%Sans-serif font outside centering, as expected.
\end{document}
为什么 给出的标题\myhead{}
用 呈现\normalfont
,而不是按\sffamily
指定方式呈现?这里的规则是什么?
答案1
\bf
不维护字体系列,除非在明确的组{...}
或环境中使用。
使用更安全的\bfseries
方法以确保万无一失!
这就是为什么不应再使用\bf
、\it
、\sc
和\tt
的原因之一——它们自 1993 年起就已被弃用。\rm
请参阅以下代码输出中的差异:(但是,我没有清理!)
\documentclass{article}
\newcommand{\myhead}[1]{\noindent{\large\bfseries #1}\vspace{2mm}}
\newcommand{\myotherhead}[1]{\noindent{\large\bf #1}\vspace{2mm}}
\begin{document}
\sffamily%%%Change to sans-serif font
\title{\textbf{This is a sans-serif title...}}%%%as expected
\author{\textbf{...and author...}}%%%as expected
\date{\textbf{...and date.}}%%%as expected
\clearpage\maketitle
\begin{center}%%%note that centering has its own scope regarding fonts.
\ttfamily%%%typewriter within scope
This is an url here.%%%as expected
\normalfont%%%normalfont within scope
\normalsize%%%normalfont within scope
\noindent This is normal text here, as expected.%%%as expected
\end{center}
\myhead{This heading is written in normal text, what you would intuitively expect. However, your intuition is wrong...}
%%%At first you would think that \normalfont above changes the font back to normal. But this is wrong, since it is
%%%within a \begin{center}\end{center} scope. So what you would expect that this text here will be sans-serif.
%%%But it isn't!
...since this bold text is still written with a sans-serif font now.%%%Sans-serif font outside centering, as expected.
\myotherhead{This heading is written in normal text, what you would intuitively expect. However, your intuition is wrong...}
%%%At first you would think that \normalfont above changes the font back to normal. But this is wrong, since it is
%%%within a \begin{center}\end{center} scope. So what you would expect that this text here will be sans-serif.
%%%But it isn't!
...since this bold text is not written with a sans-serif font!
\end{document}
更新
\bf
显示切换回罗马字体的一个小示例文档:
\documentclass{article}
\newcommand\sampletext{The quick brown fox jumps}
\makeatletter
\newcommand{\showfamily}{\f@family}
\makeatother
\begin{document}
\sffamily
\sampletext
Output: \showfamily
\bf \sampletext
Output after \verb!\bf!: \showfamily
\end{document}