3

I wanted to create a puzzle book with 3 parts, Problems, Hints and Solutions. Both LaTeX-Community and TeX.SX have helped me in figuring that out.

I want to do two things:

  1. To centre the Part names on their title page.
  2. I wanted to have each problem and each solution start on a new page but hints could be continuous.

egregs base code is given below:

\documentclass{article}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{environ}

\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}

\newcounter{problem}
\newenvironment{problem}[1]
{\refstepcounter{problem}%
{\textbf{Problem \theproblem\ (#1)}\par}\nopagebreak\noindent\ignorespaces}
{}

\makeatletter
\newtoks\late@hintstoks
\newtoks\late@solutiontoks

\NewEnviron{hints}{%
\toks@=\expandafter{\BODY}%
\protected@edef\@tempa{%
\the\late@hintstoks % the previous contents
\noexpand\late@hint{\theproblem}{\the\toks@}%
}%
\global\late@hintstoks=\expandafter{\@tempa}%
}
\NewEnviron{solution}{%
\toks@=\expandafter{\BODY}%
\protected@edef\@tempa{%
\the\late@solutiontoks % the previous contents
\noexpand\late@solution{\theproblem}{\the\toks@}%
}%
\global\late@solutiontoks=\expandafter{\@tempa}%
}
\newcommand{\late@hint}[2]{%
\paragraph*{Hints for problem #1}#2%
}
\newcommand{\late@solution}[2]{%
\subsubsection*{Solution for problem #1}#2%
}

\newcommand{\printhints}{%
\part{Hints}
\the\late@hintstoks
}
\newcommand{\printsolutions}{%
\part{Solutions}
\the\late@solutiontoks
}
\makeatother

\begin{document}

\part{Problems}
\section{Puzzle 1}
\begin{problem}{Pythagoras}
This problem is about Pythagoras' theorem
\begin{hints}
These are the hints about Pythagoras.
\end{hints}
\begin{solution}
Pythagoras' theorem is easy!
\end{solution}
\end{problem}
\section{Puzzle 2}
\begin{problem}{Riemann}
Prove the zeros of the $\zeta$ functions are on the critical line.
\begin{hints}
Try first with a few cases.
\end{hints}
\begin{solution}
Oh, well!
\end{solution}
\end{problem}

\printhints

\printsolutions

\end{document}

This question is also posted at Title Centering and \newpage command inside \newcommand.

3 Answers3

3

You can center the part by using sectsty and adding

\usepackage{sectsty}
\partfont{\centering}

And to have things on new page, you have to add some \clearpage/\cleardoublepage (just to be safe) as I have marked in the code.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{environ}
\usepackage{sectsty}
\partfont{\centering}

\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}

\newcounter{problem}
\newenvironment{problem}[1]
{\refstepcounter{problem}%
{\textbf{Problem \theproblem\ (#1)}\par}\nopagebreak\noindent\ignorespaces}
{\clearpage}                                          %%%<--- here

\makeatletter
\newtoks\late@hintstoks
\newtoks\late@solutiontoks

\NewEnviron{hints}{%
\toks@=\expandafter{\BODY}%
\protected@edef\@tempa{%
\the\late@hintstoks % the previous contents
\noexpand\late@hint{\theproblem}{\the\toks@}%
}%
\global\late@hintstoks=\expandafter{\@tempa}%
}
\NewEnviron{solution}{%
\toks@=\expandafter{\BODY\clearpage}%                   %%%<--- here
\protected@edef\@tempa{%
\the\late@solutiontoks % the previous contents
\noexpand\late@solution{\theproblem}{\the\toks@}%
}%
\global\late@solutiontoks=\expandafter{\@tempa}%
}
\newcommand{\late@hint}[2]{%
\paragraph*{Hints for problem #1}#2%
}
\newcommand{\late@solution}[2]{%
\subsubsection*{Solution for problem #1}#2%
}

\newcommand{\printhints}{%
\cleardoublepage                                                %%%<--- here
\part{Hints}
\the\late@hintstoks
}
\newcommand{\printsolutions}{%
\cleardoublepage                                                 %%%<--- here
\part{Solutions}
\the\late@solutiontoks
}
\makeatother

\begin{document}

\part{Problems}
\section{Puzzle 1}
\begin{problem}{Pythagoras}
This problem is about Pythagoras' theorem
\begin{hints}
These are the hints about Pythagoras.
\end{hints}
\begin{solution}
Pythagoras' theorem is easy!
\end{solution}
\end{problem}
\section{Puzzle 2}
\begin{problem}{Riemann}
Prove the zeros of the $\zeta$ functions are on the critical line.
\begin{hints}
Try first with a few cases.
\end{hints}
\begin{solution}
Oh, well!
\end{solution}
\end{problem}

\printhints

\printsolutions

\end{document}

enter image description here

1

I used \clearpages as well to start every problem and solution on it's own page. Since i have the feeling, that this might be a longer work, i simply switched form an article class to a report class, more precisely scrreprt from the KOMA-bundle. Anybody eager grasping the loks of the standard classes, can uncomment emulatestandardclasses. But we are living in modern times.

\documentclass[%emulatestandardclasses
]{scrreprt}
\usepackage[english]{babel}
\usepackage{mathtools}
\usepackage{environ}

\numberwithin{equation}{section}
\numberwithin{figure}{section}
\numberwithin{table}{section}

\newcounter{problem}
\newenvironment{problem}[1]
{\refstepcounter{problem}%
{\clearpage\noindent\textbf{Problem \theproblem\ (#1)}\medbreak}\nopagebreak\noindent\ignorespaces}
{\bigbreak}

\makeatletter
\newtoks\late@hintstoks
\newtoks\late@solutiontoks

\NewEnviron{hints}{%
\toks@=\expandafter{\BODY}%
\protected@edef\@tempa{%
\the\late@hintstoks % the previous contents
\noexpand\late@hint{\theproblem}{\the\toks@}%
}%
\global\late@hintstoks=\expandafter{\@tempa}%
}
\NewEnviron{solution}{%
\toks@=\expandafter{\BODY}%
\protected@edef\@tempa{%
\the\late@solutiontoks % the previous contents
\noexpand\late@solution{\theproblem}{\the\toks@}%
}%
\global\late@solutiontoks=\expandafter{\@tempa}%
}
\newcommand{\late@hint}[2]{%
\paragraph*{Hints for problem #1}#2%
}
\newcommand{\late@solution}[2]{%
\clearpage
\subsection*{Solution for problem #1}#2%
}

\newcommand{\printhints}{%
\part{Hints}
\the\late@hintstoks
}
\newcommand{\printsolutions}{%
\part{Solutions}
\the\late@solutiontoks
}
\makeatother

\begin{document}

\part{Problems}
\begin{problem}{Pythagoras}
This problem is about Pythagoras' theorem
\begin{hints}
These are the hints about Pythagoras.
\end{hints}
\begin{solution}
Pythagoras' theorem is easy!
\end{solution}
\end{problem}
\begin{problem}{Riemann}
Prove the zeros of the $\zeta$ functions are on the critical line.
\begin{hints}
Try first with a few cases.
\end{hints}
\begin{solution}
Oh, well!
\end{solution}
\end{problem}

\printhints

\printsolutions

\end{document}

I asked some further questions to improve the outcome over at LaTeX-community. Everybody interested is invited to joint the discusion development.

Johannes_B
  • 24,235
  • 10
  • 93
  • 248
0

I figure out the second part. To add \newpage command to Solutions part, you add a \newpage command inside the \late@solution environment to get the desired effect.

\newcommand{\late@solution}[2]{%
  \subsubsection*{Solution for puzzle #1}#2%
  \newpage
}

To have the centering of the Part (and their title) I changed the document class to book and it works but it messes up the numbering of all the sections and it interprets everything as chapter etc. Is there a way to fix this ?