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.

I want to test if the class supports chapters. However with the package titlesec the check for the command \chapter fails because it is always defined:

\documentclass[]{scrartcl}
\usepackage{etoolbox}
\usepackage{titlesec}
\begin{document}
\ifcsdef{chapter}{chapter is defined}{chapter is NOT defined}

\chapter{title}
\end{document}

This prints out: chapter is defined

I could test before loading titlesec and create a boolean that I test, but I would rather want to test it at any point of the document with a single check. How could I do that?

share|improve this question

2 Answers 2

up vote 7 down vote accepted

From the documentation of etoolbox:

\ifdef{<control sequence>}{<true>}{<false>}
Expands to <true> if the <control sequence> is defined, and to <false> otherwise. Note that control sequences will be considered as defined even if their meaning is \relax. This command is a LaTeX wrapper for the e-TeX primitive \ifdefined.

\ifundef{<control sequence>}{<true>}{<false>}
Expands to <true> if the <control sequence> is undefined, and to <false> otherwise. Apart from reversing the logic of the test, this command also differs from \ifdef in that commands will be considered as undefined if their meaning is \relax.

\ifcsdef{<csname>}{<true>}{<false>}
Similar to \ifdef except that it takes a control sequence name as its first argument. This command is a LaTeX wrapper for the e-TeX primitive \ifcsname.

\ifcsundef{<csname>}{<true>}{<false>}
Similar to \ifundef except that it takes a control sequence name as its first argument. This command may be used as a drop-in replacement for the \@ifundefined test in the LaTeX kernel.

Thus you should use \ifcsundef (reversing the two arguments):

\ifcsundef{chapter}{chapter is NOT defined}{chapter is defined}

or

\ifundef\chapter{chapter is NOT defined}{chapter is defined}

The titlesec package performs the test \@ifundefined{chapter}{...}{...} and so \chapter becomes equivalent to \relax.

share|improve this answer

With a simple \ifChapter

\documentclass[]{scrbook}

\makeatletter
\newif\ifChapter
\@ifundefined{chapter}{\Chapterfalse}{\Chaptertrue}
\makeatletter
\ifChapter \usepackage{titlesec} \fi
\begin{document}
\ifChapter \chapter{title} \else \section{title}\fi
\ifChapter chapter is defined \else chapter is NOT defined \fi
\end{document}
share|improve this answer

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.