조건부 - 특정 명령이 문서에서 사용되는 경우 작업을 수행합니다.

조건부 - 특정 명령이 문서에서 사용되는 경우 작업을 수행합니다.

우리가 무언가를 할 수 있게 하는 조건을 갖는 것이 가능합니까?만약에주어진 명령이문서? 예를 들어 \part명령은 다음과 같습니다.

\@ifcommandisbeingused{\part}{<then>}{<else>}

설명

다음은 명령이 목차에서 사용되기 \bfseries때문에 목차의 장 항목에 대해 를 방지합니다 .\part문서.

\documentclass{book}

\makeatletter

\@ifcommandisbeingused{\part}
{  
\renewcommand*\l@chapter[2]{%
  \ifnum \c@tocdepth >\m@ne
    \addpenalty{-\@highpenalty}%
    \vskip 1.0em \@plus\p@
    \setlength\@tempdima{1.5em}%
    \begingroup
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \leavevmode
      \advance\leftskip\@tempdima
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par
      \penalty\@highpenalty
    \endgroup
  \fi}
}{}

\makeatother    

\begin{document}

\part{Lorem}
\chapter{Ipsum}

\end{document}

추신

TeX/LaTeX 언어에서는 "매우 낮은 수준" 매크로에서만 대문자를 사용하는 것이 일반적이라는 것을 기억했기 때문에 로 \@ifcommandIsBeingUsed변경 했습니다 .\@ifcommandisbeingused

답변1

\jobname.use이 솔루션은 사용된 명령을 추적하는 파일을 생성합니다 . 를 수행하여 목록에 명령을 추가합니다 \ifused\<command>{<hook>}. 이 예에서는 from을 제거하기 위해 \patchcmdfrom을 사용했습니다 . 동일한 명령을 두 번 수행하면 오류가 발생합니다.etoolbox\bfseries\l@chapter\ifused

\documentclass{book}
\usepackage{xparse,etoolbox}
\ExplSyntaxOn

\file_if_exist:nT { \c_job_name_tl . use }
 { \file_input:n { \c_job_name_tl . use } }

\iow_new:N \l_cloud_used_stream
\iow_open:Nn \l_cloud_used_stream { \c_job_name_tl . use  }
\AtEndDocument { \iow_close:N \l_cloud_used_stream }

\cs_new_protected:Npn \cloud_is_used:N #1
 {
  \iow_now:Nx \l_cloud_used_stream
   { \exp_not:N \bool_set_true:c { l _ cloud _ if \cs_to_str:N #1 _ bool } }
 }

\NewDocumentCommand{\ifused}{mm}
 {
  \preto #1 { \cloud_is_used:N #1 }
  \iow_now:Nx \l_cloud_used_stream
   { \exp_not:N \bool_new:c { l _ cloud _ if \cs_to_str:N #1 _ bool } }
  \bool_if:cT { l _ cloud _ if \cs_to_str:N #1 _ bool } { #2 }
 }

\ExplSyntaxOff

\makeatletter
\ifused\part{\patchcmd\l@chapter{\bfseries}{}{}{}}
\makeatother

\begin{document}
\tableofcontents
\chapter{Before first part}
\part{First part}
\chapter{After first part}
\part{Second part}
\end{document}

여기에 이미지 설명을 입력하세요

관련 정보