5

I try to make a command who have different behavior depending if it is executed in mainmatter area or not.

So I use the \if@mainmatter command as described in the topic Checking the mainmatter

But, when I use it in the following MWE:

\documentclass{book}

\newcommand\foo{
  \makeatletter
  \if@mainmatter
  {We are on mainmatter}
  \else%
  {We are \textbf{not} on mainmatter}
  \fi%
  \makeatother
}

\begin{document}

\frontmatter

lorem ipsum dolor \foo

\mainmatter

lorem ipsum dolor \foo

\end{document}

I get the following redering:

lorem ipsum dolor We are not on mainmatter

(page break)

lorem ipsum dolor We are not on mainmatter

The command didn’t make at all the difference between mainmatter area and no mainmatter area.

So how to use it to change the behavior of foo command depending on if it is executed on mainmater area or not?

Bernard
  • 271,350
fauve
  • 2,501

1 Answers1

5

You have to put the \makeatletter outside \newcommand.

\documentclass{book}

\makeatletter
\newcommand\foo{
  \if@mainmatter
  {We are on mainmatter}
  \else%
  {We are \textbf{not} on mainmatter}
  \fi%
}
\makeatother

\begin{document}

\frontmatter

lorem ipsum dolor \foo

\mainmatter

lorem ipsum dolor \foo

\end{document}