3

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."
ELSE
    DO "This text is not inside a section."
2
  • 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

6

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

\def\CheckSection
    {\doifmodeelse{*section}
        {inside section}
        {outside section}}

\def\CheckSubSection
    {\doifmodeelse{*subsection}
        {inside subsection}
        {outside subsection}}

\setuppapersize[A7]
\starttext

\section{Test \CheckSection\ \CheckSubSection}

Test \CheckSection\ \CheckSubSection

\subsection{Test \CheckSection\ \CheckSubSection}

Test \CheckSection\ \CheckSubSection

\stoptext

gives

Output

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

9
\documentclass{article}
\newcount\Level
\let\Part=\part\def\part{\global\Level=0\Part}
\let\Chapter=\chapter\def\chapter{\global\Level=1\Chapter}
\let\Section=\section\def\section{\global\Level=2\Section}
\let\Subsection=\subsection\def\subsection{\global\Level=3\Subsection}
\let\Subsubsection=\subsubsection\def\subsubsection{\global\Level=4\Subsubsection}

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

\part{foo}
\levelText
\section{bar}
\levelText
\subsection{baz}
\levelText

\end{document} 

enter image description here

2
  • 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
4

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

\usepackage{etoolbox}
\robustify{\fbox}

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

0

You must log in to answer this question.

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