2010-03-10 261 views
9

我想重新定義\part*命令,以便它自動添加內容行。這證明很困難,因爲我想在我的星形版本中重新使用原始的\part*命令。乳膠:重新定義星號命令

通常情況下(即用於取消星號命令)我會做這樣的:

\let\[email protected]\part 
\renewcommand\part[2][]{ 
    \[email protected][#1]{#2} 
    … rest of definition} 

也就是說,我將節省的\part\[email protected]原始定義和使用。

但是,這對於已加星號的命令不起作用,因爲它們沒有定義單個詞位(與上例中的\part命令不同)。這可歸結爲以下問題:如何保存已加星標的命令?

請注意,我已經知道如何使用suffix包中的\WithSuffix命令重新定義已加星標命令本身。這不是問題。

回答

8

沒有\part*命令。 \part命令會查看其後的下一個字符(使用\@ifstar),並根據是否存在星號來分派到執行實際工作的其他兩個例程之一。

http://www.tex.ac.uk/cgi-bin/texfaq2html?label=cmdstar

+0

「沒有'\部分*'命令。「 - 我知道。 :-(否則我就不會有這個問題了 – 2010-03-10 16:50:49

+0

那麼那麼就像你有和處理兩個版本那樣重新定義'\ part'?或者挖掘到LaTeX源代碼並重新定義潛在的星號「\ part」代碼? – 2010-03-10 16:56:28

+0

...但是這個是一個重要的提示,我現在已經開始工作了,很快就會發布解決方案 – 2010-03-10 16:57:54

3

感謝@ SMG的答案,我拼湊的作品完美的解決方案。下面是完整的源代碼,帶解釋性意見一起:

% If this is in *.tex file, uncomment the following line. 
%\makeatletter 

% Save the original \part declaration 
\let\[email protected]\part 

% To that definition, add a new special starred version. 
\WithSuffix\def\part*{ 
    % Handle the optional parameter. 
    \ifx\next[% 
    \let\next\[email protected]@star% 
    \else 
    \def\next{\[email protected]@star[]}% 
    \fi 
    \next} 

% The actual macro definition. 
\def\[email protected]@star[#1]#2{ 
    \ifthenelse{\equal{#1}{}} 
    {% If the first argument isn’t given, default to the second one. 
    \def\[email protected]@short{#2} 
    % Insert the actual (unnumbered) \part header. 
    \[email protected]*{#2}} 
    {% Short name is given. 
    \def\[email protected]@short{#1} 
    % Insert the actual (unnumbered) \part header with short name. 
    \[email protected]*[#1]{#2}} 

    % Last, add the part to the table of contents. Use the short name, if provided. 
    \addcontentsline{toc}{part}{\[email protected]@short} 
} 

% If this is in *.tex file, uncomment the following line. 
%\makeatother 

(這需要包suffixifthen

現在,我們可以用它:

\part*{Example 1} 
This will be an unnumbered part that appears in the TOC. 

\part{Example 2} 
Yes, the unstarred version of \verb/\part/ still works, too.