# Setting \pagestyle{fancy} seems into create an error

Calling fancy page style is somehow apparently causing the following condition to test true.

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}
\ifcsname chapter\endcsname%
\renewcommand{\chapter}{}
\fi
\pagestyle{empty}
\begin{document}
testing
\end{document}


If I place \pagestyle{fancy} after the conditional, \ifcsname chapter....\fi, everything works. This is an article class, so this conditional should be false. If I call \pagestyle{plain}, it doesn't matter where I place it.

Why does this order matter?

The page style fancy (\ps@fancy) contains:

\@ifundefined{chapter}{...}{...}


That is the LaTeX test for undefined:

\def\@ifundefined#1{%
\expandafter\ifx\csname#1\endcsname\relax
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi}


\csname has the side effect that it defines an undefined command with the meaning \relax:

\show\chapter
\pagestyle{fancy}% \@ifundefined{chapter}{...}{...}
\show\chapter


The meaning of \chapter changes:

> \chapter=undefined.
l.3 \show\chapter

?
> \chapter=\relax.
l.5 \show\chapter



e-TeX's \ifcsname does not have the side effect and lets the command untouchted. (Also it is internally optimized in the sense that it does not create a hash table entry.)

The test needs to be a little longer:

\ifcsname chapter\endcsname
\ifx\chapter\relax
\else
\renewcommand\chapter{}
\fi
\fi


A defined \chapter might confuse packages and macros about the capabilities of the class (article does not have chapters). If the purpose of the mysterious \renewcommand\chapter{} is to undefine chapter, then it will fail, because \chapter remains defined with the meaning of an empty macro. A command can be undefined by assigning it to an undefined macro:

\ifcsname chapter\endcsname
\let\chapter\UnDeFiNeD
\fi

• So, I gather \ifcsname does not have this side-effect? I ask because I'd already used \ifcsname chapter\endcsname at least once earlier in my original file. But there was never a complaint when compiling. – A.Ellett Jun 20 '13 at 6:47
• Actually, \renewcommand\chapter{} was just for the MWE. In the actual document I'm doing something more along the lines of what @egreg was suggesting regarding tex.stackexchange.com/a/111641/22413 an earlier question of mine). I would like my package to be versatile enough to define things correctly whether the class is article, book`, etc. – A.Ellett Jun 20 '13 at 6:52