# Test if chapter is a valid command

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?

-

From the documentation of etoolbox:

\ifdef{<control sequence>}{<true>}{<false>}
Expands to <true> if the <control sequence> is deﬁned, 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 undeﬁned, 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.

-

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}

-