为目录视图修补 \l@part 宏

为目录视图修补 \l@part 宏

我已经修补了标准书籍类宏\l@part以获得理想的视图。我希望​​在宏未加星号的位置(在下面的代码中)添加一些单词,\part如果 则不添加任何内容\part*。是否可以不使用任何附加包?可以使用或。\g@addto@macro\@part\@spart

\renewcommand*\l@part[2]{%
    \ifnum \c@tocdepth >-2\relax
        \addpenalty{-\@highpenalty}%
        \addvspace{1em \@plus\p@}%
        \setlength\@tempdima{3em}%
        \begingroup
        \parindent \z@ \rightskip \@pnumwidth
        \parfillskip -\@pnumwidth
        {\leavevmode
            {IF \part IS UNSTARED ADD SOME WORD HERE}\hrulefill\nopagebreak\par\medskip   
            \large \bfseries \vbox{\centering\sffamily\bfseries#1}%
            %\hfil \hb@xt@\@pnumwidth{\hss #2}
        }
        \nopagebreak\hrulefill\par
        \vspace*{1ex}
        \nobreak
        \global\@nobreaktrue
        \everypar{\global\@nobreakfalse\everypar{}}%
        \endgroup
    \fi
    }

我的MWE下面

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\makeatletter
\renewcommand*\l@part[2]{%
    \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{1em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
    \parindent \z@ \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    {\leavevmode
        \hrulefill\nopagebreak\par\medskip   
        \large \bfseries \vbox{\centering\sffamily\bfseries#1}%
        %\hfil \hb@xt@\@pnumwidth{\hss #2}
    }
    \nopagebreak\hrulefill\par
    \vspace*{1ex}
    \nobreak
    \global\@nobreaktrue
    \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
    \fi
}

\def\partmark{}



\def\@part[#1]#2{%
    \renewcommand\partmark{#1}
    \thispagestyle{empty}
    \addcontentsline{toc}{part}{#1}%
    \markboth{}{}%
    {\centering
        \interlinepenalty \@M
        \vskip 20\p@
        \Huge\sffamily\bfseries #2\par
        \vskip 20\p@
        \hrulefill
    }%
    \@endpart
    }

\def\@spart#1{%
    \renewcommand\partmark{#1}
    \thispagestyle{empty}
\addcontentsline{toc}{part}{#1}%%
    {\centering
        \interlinepenalty \@M
        \normalfont
        \Huge \sffamily\bfseries #1\par}%
    \@endpart
}
\makeatother


\begin{document}
\tableofcontents

\part*{Stared}
\chapter{ABC}
\lipsum[1]

\part{UnStared}
\chapter{DEF}
\lipsum[1]

\end{document}

答案1

最简单的方法是使用conditional,假设如果使用则\ifnotstarredversion设置为,否则为。true\partfalse

但是,条件的状态必须用 写入ToC文件\addtocontents{toc}{\protect\notstarredtrue},因为必须在生成 时对其进行评估,而不是在等使用ToC时进行评估。\part\addcontentsline

使用宏\partleadinword(或任何你喜欢的称呼)来保存单词等的内容,如果使用的话,这些内容应该放在行的开头\part

\documentclass[a4paper,12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}


\makeatletter

\newif\ifnotstarredversion



\newcommand{\partleadinword}{Please provide a MWE ;-)}

\renewcommand*\l@part[2]{%
    \ifnum \c@tocdepth >-2\relax
    \addpenalty{-\@highpenalty}%
    \addvspace{1em \@plus\p@}%
    \setlength\@tempdima{3em}%
    \begingroup
    \parindent \z@ \rightskip \@pnumwidth
    \parfillskip -\@pnumwidth
    {\leavevmode
      \ifnotstarredversion
      \partleadinword%
      \fi
      \hrulefill\nopagebreak\par\medskip   
      \large \bfseries \vbox{\centering\sffamily\bfseries#1}%
      % \hfil \hb@xt@\@pnumwidth{\hss #2}

    }
    \nopagebreak\hrulefill\par
    \vspace*{1ex}
    \nobreak
    \global\@nobreaktrue
    \everypar{\global\@nobreakfalse\everypar{}}%
    \endgroup
    \fi
}

\def\partmark{}



\def\@part[#1]#2{%
  \renewcommand\partmark{#1}
  \thispagestyle{empty}
  \addtocontents{toc}{\protect\notstarredversiontrue}
  \addcontentsline{toc}{part}{#1}%
  \markboth{}{}%
  {\centering
    \interlinepenalty \@M
        \vskip 20\p@
        \Huge\sffamily\bfseries #2\par
        \vskip 20\p@
        \hrulefill
      }%
      \@endpart
    }

\def\@spart#1{%
    \renewcommand\partmark{#1}
    \thispagestyle{empty}
    \addtocontents{toc}{\protect\notstarredversionfalse}
    \addcontentsline{toc}{part}{#1}%%
    {\centering
        \interlinepenalty \@M
        \normalfont
        \Huge \sffamily\bfseries #1\par}%
    \@endpart
}
\makeatother


\begin{document}
\tableofcontents

\part*{Intro}
\chapter{ABC}
\lipsum[1]

\part{One}
\chapter{DEF}
\lipsum[1]

\end{document}

相关内容