问题解答和问题解答文档

问题解答和问题解答文档

在下面的代码中,文档中有两部分。第一部分包含一个 Question 然后是 Answer,第二部分包含一个 Problem 然后是 Answer。

对于第 1 部分,我希望将文档格式化,使其看起来像这样:

问题 1

Q1 问题 1

...

AQ1 答案 1

...

问题2

Q2 问题 2

...

AQ2 答案 2

...

那么对于第 2 部分,格式应如下所示:

问题 1

P1 问题 1

...

AP1 答案 1

...

问题 2

P2 问题 2

...

AP2 答案 2

...

以下是目前编写的代码:

\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

这使用一个开关来控制各个样式,\questionanswer用于第一部分和\problemanswer第二部分。

检查\if@questionmode当前section是问题还是答案——假设问题之间没有间隙!

\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} 

相关内容