4

Is it possible to have a conditional that allow us to do something if a given command is being used in the document? For example for the \part command would be:

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

Clarification

The following would avoid the \bfseries for the chapter entry in the TOC, because the \part command is being used in the document.

\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}

P.S.

I've changed \@ifcommandIsBeingUsed to \@ifcommandisbeingused 'cause I've remembered that in TeX/LaTeX language is common to use uppercase letters only in "very low level" macros.

5
  • 3
    Note that usage of such a command would at least require two compiles since you may want to use \@ifcommandIsBeingUsed before it is actually used...
    – Werner
    Commented Mar 17, 2014 at 20:55
  • LaTeX is turring-complete, so yes. I recently used the ifthen package in an answer here: tex.stackexchange.com/questions/165873/…
    – Argo
    Commented Mar 17, 2014 at 21:23
  • To clarify, you wish this command to discern whether a macro has actually been invoked, and not just if it has been defined. Is that correct? Commented Mar 17, 2014 at 22:17
  • 4
    Also, when you say "is being used", do you mean "will be used at some point" or "has been used to this point" in the document? Commented Mar 17, 2014 at 22:41
  • @StevenB.Segletes I hope my update will answer your questions.
    – Aurelius
    Commented Mar 17, 2014 at 23:34

1 Answer 1

4

This solution creates a file \jobname.use which tracks, which commands have been used. You add a command to the list by doing \ifused\<command>{<hook>}. In the example I used \patchcmd from etoolbox to remove \bfseries from \l@chapter. Doing \ifused twice for the same command will produce errors.

\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}

enter image description here

4
  • 1
    \preto =? \tl_put_left:Nn?
    – Manuel
    Commented Sep 14, 2014 at 14:19
  • @Manuel Indeed that works, but it shadows the initial intention. Unfortunately there is no \cs_put_left:Nn. Commented Sep 14, 2014 at 14:36
  • \cs_put_left:Nn would be more like \pretocmd. I think \tl_put_left:Nn is fine here, but it's true what you say :)
    – Manuel
    Commented Sep 14, 2014 at 14:38
  • 1
    Note that \c_job_name_tl has been replaced by \c_sys_jobname_str in the September 2015 release of expl3.
    – egreg
    Commented Sep 30, 2015 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .