為什麼 \shorthandon 和 \shorthandoff 不能嵌入其他巨集中?

為什麼 \shorthandon 和 \shorthandoff 不能嵌入其他巨集中?

用and包圍\title宏給出了's語言的預期結果(在 '?'、'!'、':' 和 ';' 之前添加了空格):\shorthandon{;:!?}\shorthandoff{;:!?}babelfrench

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
%
\shorthandon{;:!?}%
\title{La crise? Quelle crise?}
\shorthandoff{;:!?}%
%
\begin{document}
\maketitle
\end{document}

\shorthandon但是,如果和\shorthandoff嵌入到巨集(重新定義)中,則這不起作用\title

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}
%
\let\titleORI\title
\renewcommand{\title}[1]{%
  \shorthandon{;:!?}%
  \titleORI{#1}%
  \shorthandoff{;:!?}%
}
%
\title{La crise? Quelle crise?}
\author{Un auteur? Deux auteurs!}
%
\begin{document}
\maketitle
\end{document}

這是什麼原因呢?有解決方法嗎?

答案1

說話\shorthandon{?}可以塑造?活躍的性格。因此,當 的參數\title被吸收時,在第一個範例中,它是活動的,並且在 處\maketitle,LaTeX 將使用它的當前定義,這恰好是由 定義的babel-french

相反,在第二種情況下,\shorthandon當 的參數\title已被吸收時,命令將被執行,因此?不處於活動狀態,因此它將永遠保留(在 的替換文字中,這是儲存標題\@title的巨集)。\title

您必須在\shorthandon執行後延遲讀取參數。

\documentclass[french]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{babel}

\let\ORItitle\title
\renewcommand{\title}{%
  \shorthandon{;:!?}%
  \titlewithshorthand
}
\newcommand{\titlewithshorthand}[1]{%
  \ORItitle{#1}%
  \shorthandoff{;:!?}%
}

\title{La crise? Quelle crise?}

\begin{document}
\maketitle
\end{document}

但我只是簡單地\title放在\begin{document}.

相關內容