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:



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



  \ifnum \c@tocdepth >\m@ne
    \vskip 1.0em \@plus\p@
      \parindent \z@ \rightskip \@pnumwidth
      \parfillskip -\@pnumwidth
      \hskip -\leftskip
      #1\nobreak\hfil \nobreak\hb@xt@\@pnumwidth{\hss #2}\par






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.

  • 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 Mar 17 '14 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 Mar 17 '14 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? – Steven B. Segletes Mar 17 '14 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? – Steven B. Segletes Mar 17 '14 at 22:41
  • @StevenB.Segletes I hope my update will answer your questions. – Aurelius Mar 17 '14 at 23:34

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.


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

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



\chapter{Before first part}
\part{First part}
\chapter{After first part}
\part{Second part}

enter image description here

  • 1
    \preto =? \tl_put_left:Nn? – Manuel Sep 14 '14 at 14:19
  • @Manuel Indeed that works, but it shadows the initial intention. Unfortunately there is no \cs_put_left:Nn. – Henri Menke Sep 14 '14 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 Sep 14 '14 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 Sep 30 '15 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.