7

I am trying to create a conditional function to determine whether a new chapter has been created (for a different purpose than that of the MWE). So far I have been unsuccessful, despite having tryed different things.

The code first checks whether a command, that stores the current chapter counter, has been created in the first place. If it has been, it will then try to compare the current chapter counter with the stored one.

The expected output for this MWE is:

The command \ifnewchaptercreated returns "New chapter: Chapter 1" if called for the first time in chapter 1, and "Chapter 1 already created." if called more than once. Then, if the command is called in a new chapter, it will write "New chapter: Chapter 2" when it is called for the first time, and "Chapter 2 already created." all the other times.

\documentclass[a4paper,10pt]{report}
\usepackage{fontspec}
\usepackage{ifthen}

\newcommand\ifnewchaptercreated{\ifthenelse{\isundefined\storecurrentchapter}{%
\newcommand\storecurrentchapter{\arabic{chapter}}
New chapter: Chapter \arabic{chapter}\par
}%
{\ifthenelse{\equal{\arabic{chapter}}{\storecurrentchapter}}{
New chapter: Chapter \arabic{chapter}.\par
}{ %
Chapter \arabic{chapter} already created.\par
} %
}}

\usepackage[linktoc=all]{hyperref}

\begin{document}

\chapter{A chapter}

\ifnewchaptercreated
\ifnewchaptercreated

\chapter{Another chapter}

\ifnewchaptercreated
\ifnewchaptercreated

\end{document}
  • 1
    What is your expected output in the above case? I don't understand the conditioning you have coded... – Werner Nov 28 '15 at 20:48
  • @Werner See edited post if it helps. I am trying to create a function that does something only once a chapter with conditionnal statements. But I clearly do not understand how it works ... – HcN Nov 28 '15 at 20:55
6

Here is an abridged version of what you're trying to achieve:

enter image description here

\documentclass{report}

\newif\ifnewchapter% Create conditional
\newchapterfalse% Set conditional to false
\let\oldchapter\chapter
\renewcommand{\chapter}{%
  \global\newchaptertrue% A new \chapter has been created
  \oldchapter}% Regular old \chapter

\newcommand\ifnewchaptercreated{%
  \ifnewchapter% Check if new \chapter is created
    New chapter created%
    \global\newchapterfalse% Set conditional to false
  \else% New \chapter has not been created (or already used)
    No new chapter created%
  \fi
}

\begin{document}

\chapter{A chapter}

\ifnewchaptercreated \par
\ifnewchaptercreated \par
\ifnewchaptercreated \par
\ifnewchaptercreated

\chapter{Another chapter}

\ifnewchaptercreated \par
\ifnewchaptercreated

\end{document}

The approach is to "intercept" \chapter and set a condition - \ifnewchapter - to TRUE. Then, upon the use of \ifnewchaptercreated, we check the conditional and reset it accordingly.

6

You can use the etoolbox package to

  1. create a toggle to be switched on when you call \chapter

  2. use the toggle to determine whether a chapter has been created. The trick is that the command that does the check sets the toggle to false

Here is how to do that

\documentclass[11pt]{report}

\usepackage{etoolbox}

\newtoggle{chaptercreated}
\preto{\chapter}{\toggletrue{chaptercreated}}

\newcommand{\ifnewchaptercreated}{%
  \iftoggle{chaptercreated}
    {Last chapter created \thechapter{}}
    {Chapter \thechapter{} already created}%
  \togglefalse{chaptercreated}%
}

\begin{document}
\chapter{A chapter}

\ifnewchaptercreated
\ifnewchaptercreated

\chapter{Another chapter}

\ifnewchaptercreated
\ifnewchaptercreated
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.