Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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."
share|improve this question
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 Nov 12 '11 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? –  Martin Scharrer Nov 12 '11 at 8:30
add comment

3 Answers

up vote 5 down vote accepted

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

share|improve this answer
add comment
\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

share|improve this answer
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. –  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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

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.