How can I make a conditional that checks if it is currently inside a \part, \chapter, or \section?

IF inside sectioning = TRUE
    DO "This text is inside a section."
    DO "This text is not inside a section."
  • 2
    What is the end use here? Something like \section{blah blah \mymacro{<stuff>} blah blah} where <stuff> is evaluated in a certain way depending on whether you're in a \section or not. Better yet, provide a more detailed description of the setting using a code example that forms part of a standard document.
    – Werner
    Commented Nov 12, 2011 at 8:28
  • 1
    The sectioning text is a so called moving argument. It is placed multiple times: first to create the sectioning header, then it is written to the .toc file so that it appears in the ToC. It might also be used for PDF bookmarks in a similar way. I assume you want to have this conditional to test for potential problems with the expansion when written to the .aux file, right? Commented Nov 12, 2011 at 8:30

3 Answers 3


ConTeXt provides system modes *section, *subsection, etc that are active inside section heads. For example

        {inside section}
        {outside section}}

        {inside subsection}
        {outside subsection}}


\section{Test \CheckSection\ \CheckSubSection}

Test \CheckSection\ \CheckSubSection

\subsection{Test \CheckSection\ \CheckSubSection}

Test \CheckSection\ \CheckSubSection




Note that these modes are inactive when displaying table of contents. If you want to check if you are inside a table of contents (or list, in ConTeXt terminology), check for *list mode. Similarly, you can check for *marking mode to see if you are inside a marking (header or footer), and check for *register to see if you are inside a register (index, etc.).


\def\levelText{i am inside a 
  \ifcase\the\Level part
  \or chapter
  \or section
  \or subsection
  \or subsubsection
  \else default text \fi}



enter image description here

  • 5
    I personally understood the question that the test should be if the macro is inside the argument of a sectioning command. But the question is vague, so your interpretation might be also the right one. Commented Nov 12, 2011 at 8:39
  • Herbert: To keep the user from accidentally using the newly-defined \Part, \Chapter, etc instructions, I suggest you encase the five \let... instructions inside \makeatletter ... \makeatother and endow each of the uppercase-letter macros with an @ symbol.
    – Mico
    Commented Nov 12, 2011 at 12:50

If your problem is related to this other question from you and consists in providing automatically the \protect before \fbox, you can simply say


so that any later usage of \fbox won't require the \protect even inside moving arguments such as a section title.


You must log in to answer this question.

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