來自source2e.pdf

來自source2e.pdf

的定義\@setfontsize看起來像這樣( 的輸出\makeatletter\meaning\@setfontsize

\@nomath#1\ifx\protect\@typeset@protect\let\@currentsize#1\fi\fontsize{#2}{#3}\selectfont
  • \@nomath如果您在數學模式下呼叫此宏,則會發出警告。參見來源2e。
  • \let\@currentsize#1不在我的source2e 中。我的(v. 2017/01/01 補丁等級 1)有\@currsize#1
  • \fontsize{fontsize}{baselineskip}

揭示產量\@typeset@protect(的輸出\makeatletter\meaning\@typeset@protect\relax

來自source2e.pdf

為了確保 \@setfontsize 保持穩健,我們在 \protect 與 \@typeset@protect 不同時省略此分配。

  • 那些時間是什麼時候?
  • 什麼時候將其設定為其他內容\relax以及出於什麼目的?
  • 順便說一句,“保持穩健”是一個有趣的措辭

答案1

\documentclass{article}

\begin{document}

\typeout{\large abc}

\end{document}

產生一個日誌

\relax \fontsize {12}{14}\selectfont abc

這並不是特別有趣,但它是相當於\large在實際應用程式中寫入auxtoc檔案的程式碼,讀回時它會按預期工作。

如果我們修改定義以刪除\ifx\protect\@typeset@protect測試,那麼效果相當顯著:

\documentclass{article}

\begin{document}


\makeatletter
\def\@setfontsize#1#2#3{\@nomath#1%
%    \ifx\protect\@typeset@protect
      \let\@currsize#1%
%    \fi
    \fontsize{#2}{#3}\selectfont}

\typeout{\large abc}

\end{document}

產生

! TeX capacity exceeded, sorry [input stack size=5000].
\@nomath ...e \@font@warning {Command \noexpand #1
                                                  invalid in math mode}\fi 
l.13 \typeout{\large abc}

!  ==> Fatal error occurred, no output PDF file produced!

基本上,測試確保字體大小命令是健壯的而不是脆弱的。

相關內容