Text FormattingTitle Centering and \newpage command inside \newcommand

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
CutestPenguin
Posts: 4
Joined: Tue Jan 13, 2015 4:23 am

Title Centering and \newpage command inside \newcommand

Post by CutestPenguin »

I wanted to create a puzzle book with 3 parts, Problems, Hints and Solutions. Both LaTeX Community and TeX.stackexchange have helped me in figuring that out. I wanted 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.
The base code is given below:

Code: Select all

\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}
Last edited by CutestPenguin on Sat Jan 24, 2015 5:13 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

CutestPenguin
Posts: 4
Joined: Tue Jan 13, 2015 4:23 am

Title Centering and \newpage command inside \newcommand

Post by CutestPenguin »

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.

Code: Select all

\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 ?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Title Centering and \newpage command inside \newcommand

Post by Johannes_B »

Harish Kumar used package sectsty to center the part headers.
In the following example, i changed the documentclass form article to report (from the KOMA-bundle, for a bit longer works).

To begin each solution on its own page, i used \clearpage instead of \newpage to ensure that any floating objects like figures or tables are first output and don't get flush into the next section/solution.

Code: Select all

\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}
It might be a good idea to tell us a bit more about what you are trying to achieve. Is it going to be for a fairly short exam paper, then article will be good. A bit longer, for a lecture course? Report will be fine. A real text book? Might want to take a look at the book/scrbook class.
You want every problem to start on a new page, how long is a single problem?


The reason for asking this is simple. You have to look a bit into the future. I used a simple hand made heading, egreg used subsections. You can change the visual appearance of both, but egregs solution will make a toc available. On the other hand, if you want to print a list of problems and/or a list of solutions, the section-approach will not be ideal. Knowing about this stuff will save a lot of time later reimplementing stuff for new requirements.

TeX.Stackexchange is great for very specific questions. A forum like LaTeX-community is much more suited for discussions, where on solution leads to a next requirement. This can lead to the development of quite noce results. The disadvantage is, that future users on the search for a solution have to read a lot of text. So both sites have there advantages and disadvantages.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply