1

In the code below, there are two parts in the document. The first part contains a Question then Answer, and the second part contains a Problem then Answer.

For Part 1, I wanted to format the document such that it will appear like:

Question 1

Q1 Question 1

...

AQ1 Answer 1

...

Question 2

Q2 Question 2

...

AQ2 Answer 2

...

Then for Part 2, the format should appear like:

Problem 1

P1 Problem 1

...

AP1 Answer 1

...

Problem 2

P2 Problem 2

...

AP2 Answer 2

...

Here is the code that is written thus far:

\documentclass{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,letterpaper]{geometry}
\usepackage{rotating, ltablex, dcolumn}
\usepackage{lipsum}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

\usepackage{titletoc}

\begin{document}

\mainmatter

\part{Questions}
\chapter{Question 1}
\section{Question 1 }
\section{Answer 1 }
\chapter{Question 2}
\section{Question 2 }
\section{Answer 2 }
\chapter{Question 3}
\section{Question 3}
\section{Answer 3}
\chapter{Question 4}
\section{Question 4}
\section{Answer 4}
\chapter{Question 5}
\section{Question 5}
\section{Answer 5}
\chapter{Question 6}
\section{Question 6}
\section{Answer 6}
\chapter{Question 7}
\section{Question 7}
\section{Answer 7}

\part{Problems}
\chapter{Problem 1}
\section{Problem 1}
\section{Answer 1}
\chapter{Problem 2}
\section{Problem 2}
\section{Answer 2}
\chapter{Problem 3}
\section{Problem 3}
\section{Answer 3}
\chapter{Problem 4}
\section{Problem 4}
\section{Answer 4}

\end{document} 
  • 1
    Is your code minimal? – cmhughes Jul 20 '16 at 4:30
  • IMHO this is the job of enumerate not sectionning see enumitem – touhami Jul 20 '16 at 7:33
1

This uses a switch for the individual styles, \questionanswer for the first part and \problemanswer for the 2nd part.

The \if@questionmode checks whether the current section is the question or the answer -- no gaps between questions are assumed!

\documentclass{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,letterpaper]{geometry}
\usepackage{rotating, ltablex, dcolumn}
\usepackage{lipsum}
\usepackage[english]{babel}
\usepackage{booktabs}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

\usepackage{titletoc}% 

\usepackage{xparse}
\usepackage{chngcntr}
\counterwithout{section}{chapter}
\counterwithin{section}{part}

\makeatletter
\let\latex@@thesection\thesection
\newif\if@questionmode

\newif\if@problemanswermode

\newcommand{\problemprefix}{P}
\newcommand{\problemanswerprefix}{A\problemprefix}
\newcommand{\questionprefix}{Q}
\newcommand{\questionanswerprefix}{A\questionprefix}


\DeclareRobustCommand{\questionanswerformat}{%
    \if@questionmode%
    \questionprefix\arabic{section}%
    \global\@questionmodefalse
    \else
    \addtocounter{section}{-1}%
    \questionanswerprefix\arabic{section}%
    \global\@questionmodetrue
    \fi
}


\DeclareRobustCommand{\problemanswerformat}{%
    \if@questionmode%
    \problemprefix\arabic{section}%
    \global\@questionmodefalse
    \else
    \addtocounter{section}{-1}%
    \problemanswerprefix\arabic{section}%
    \global\@questionmodetrue
    \fi
}

\newcommand{\questionanswer}{%
  \@questionmodetrue
  \renewcommand{\thesection}{%
    \questionanswerformat%
  }%
}

\newcommand{\problemanswer}{%
  \@questionmodetrue
  \renewcommand{\thesection}{%
    \problemanswerformat%
  }%
}

\makeatother

\usepackage{hyperref}

\begin{document}
\tableofcontents

\mainmatter

\part{Questions}
\questionanswer
\chapter{Question 1}
\section{Question 1 }
\section{Answer 1 }
\chapter{Question 2}
\section{Question 2 }
\section{Answer 2 }
\chapter{Question 3}
\section{Question 3}
\section{Answer 3}
\chapter{Question 4}
\section{Question 4}
\section{Answer 4}
\chapter{Question 5}
\section{Question 5}
\section{Answer 5}
\chapter{Question 6}
\section{Question 6}
\section{Answer 6}
\chapter{Question 7}
\section{Question 7}
\section{Answer 7}

\part{Problems}
\problemanswer
\chapter{Problem 1}
\section{Problem 1}
\section{Answer 1}
\chapter{Problem 2}
\section{Problem 2}
\section{Answer 2}
\chapter{Problem 3}
\section{Problem 3}
\section{Answer 3}
\chapter{Problem 4}
\section{Problem 4}
\section{Answer 4}

\end{document} 
|improve this answer|||||

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.