TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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."
share|improve this question
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 Nov 12 '11 at 8:28
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? – Martin Scharrer Nov 12 '11 at 8:30
up vote 5 down vote accepted

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.).

share|improve this answer

\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

share|improve this answer
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. – Martin Scharrer Nov 12 '11 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 Nov 12 '11 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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