How to configure fancyhdr depending on the document class [duplicate]

Possible Duplicate:
How to configure theorems depending on the document class?

I would like to have if the book class is loaded but if the article class is loaded. Can I do this with some sort of conditional?

I tried, but failed.

\makeatletter%
\@ifclassloaded{book}{%
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhead{}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\fancyhead[LE]{\bfseries\leftmark}
\fancyhead[RO]{\bfseries\rightmark}
\fancyfoot{}
\fancyfoot[LE,RO]{\bfseries\thepage}
\fancyfoot[LO,RE]{Régis}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\addtolength{\headheight}{0.5pt}
\fancypagestyle{plain}{%
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
}
\typeout{Using book class.}%
}{%
\@ifclassloaded{article}{%
%   \renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhead{}
%   \renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\fancyhead[R]{\bfseries\rightmark}
\fancyfoot{}
\fancyfoot[R]{\bfseries\thepage}
\fancyfoot[L]{Régis}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\addtolength{\headheight}{0.5pt}
\fancypagestyle{plain}{%
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
}
\typeout{Using article class.}%
}{%
\@ifclassloaded{report}{%
\renewcommand{\chaptermark}[1]{\markboth{#1}{}}
\renewcommand{\sectionmark}[1]{\markright{\thesection\ #1}}
\fancyhead{}
\renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}}
\fancyhead[L]{\bfseries\leftmark}
\fancyhead[R]{\bfseries\rightmark}
\fancyfoot{}
\fancyfoot[R]{\bfseries\thepage}
\fancyfoot[L]{Régis}
\renewcommand{\headrulewidth}{0.5pt}
\renewcommand{\footrulewidth}{0.5pt}
\addtolength{\headheight}{0.5pt}
\fancypagestyle{plain}{%
\fancyhead{}
\renewcommand{\headrulewidth}{0pt}
}
\typeout{Using report class.}%
}{%
\typeout{Error: Unsupported class: 'article' or 'report' or 'book' are suported.}\QUITHERE
}%
}%
\makeatother%

-

marked as duplicate by Caramdir, Alan Munn, Werner, frabjous, SeamusAug 12 '11 at 10:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Didn't we just answer an identical question of yours like this: How to configure theorems depending on the documentclass? This is (another) duplicate of Which document class is being used? – Alan Munn Aug 11 '11 at 20:07
Alan is correct. Just inserting the appropriate sections of your code into the solution at How to configure theorems depending on documentclass should work. – Peter Grill Aug 11 '11 at 20:13
I tried, but failed. – Regis da Silva Aug 12 '11 at 3:55
can not you have two environments \makeatletter% on the same file? – Regis da Silva Aug 12 '11 at 4:13
The #1 is intended to refer to the first parameter to a macro call. Here you are not defining a macro so it doesn't have meaning. – Peter Grill Aug 12 '11 at 4:13

1 Answer

This should get you started. Just add the appropriate settings to BookHeader, ArticleHead, and ReportHead. Within these three you can use #1 to refer to what was passed to \SetHeaders.

The following is intended to exit if a class other than book or article or report is used. You can remove the \QUITHERE macro if that is not desired.

\documentclass{article}

\newcommand*{\BookHeader}[1]{% Add any customizations for book class here.
\typeout{Using book class.}%
}%

\newcommand*{\ArticleHead}[1]{% Add any customizations for article class here.
\typeout{Using article class.}%
}%

\newcommand*{\ReportHead}[1]{% Add any customizations for report class here.
\typeout{Using report class.}%
}%

\makeatletter%
\newcommand*{\SetHeaders}[1]{%
\@ifclassloaded{book}{\BookHeader{#1}%
}{\@ifclassloaded{article}{\ArticleHead{#1}%
}{\@ifclassloaded{report}{\ReportHead{#1}}{%
\typeout{Error: Unsupported class: 'article' or 'report' or 'book' are suported.}\QUITHERE
}}}%
}%
\makeatother%

\SetHeaders{foo}%

\begin{document}
Testing...
\end{document}

-
Thanks Peter Grill. – Regis da Silva Aug 12 '11 at 4:41
This is definitely easier to read code. (The original code also worked; see my comment above.) Also, @Regis Peter has the pseudo-code macro \QUITHERE; this document will fail inelegantly if an unsupported document class is used. – Alan Munn Aug 12 '11 at 5:20
How to use \renewcommand{\chaptermark}[1]{\markboth{\thechapter.\ #1}{}} for appear 1.foo (name of chapter) without CHAPTER 1.foo. Is failed in Report. – Regis da Silva Aug 12 '11 at 5:24
@Alan: Thanks, have added the comment about \QUITHERE. I find it very useful to exit for cases that I have not yet handled in the code. – Peter Grill Aug 12 '11 at 5:25
It's also possible to use \PackageError{<package name>}{<error text>}{<help text>} inside a document. – Alan Munn Aug 12 '11 at 6:05