%%
%% This is file `qqz.sty'.  It handles the latex portions of extracting
%%	Quick Quizzes.  Other pieces of the puzzle may be found
%%	in the Makefile and the utilities directory.
%%
%% \QuickQuizChapter{chp:label} -- to introduce a chapter with quick quizzes.
%% \QuickQuiz{
%%	Question goes here.
%% }\QuickQuizAnswer{
%%	Answer goes here.
%% }\QuickQuizEnd
%% \QuickQuizAnswers -- to dump out all questions and answers in new section.
%%
%% For consecutive Quick Quizzes, use the following construct:
%%
%% \QuickQuizSeries{%
%% \QuickQuizB{
%%	Question 1
%% }\QuickQuizAnswerB{
%%	Answer 1
%% }\QuickQuizEndB
%% %
%% \QuickQuizM{
%%	Question 2
%% }\QuickQuizAnswerM{
%%	Answer 2
%% }\QuickQuizEndM
%% %
%% \QuickQuizE{
%%	Question 3
%% }\QuickQuizAnswerE{
%%	Answer 3
%% }\QuickQuizEndE
%% }                 % End of \QuickQuizSeries
%%
%% To keep quizzes essential to the discussion in "noqq" build,
%% use the following:
%%
%% \EQuickQuiz{
%%	Question
%% }\EQuickQuizAnswer{
%%	Answer
%% }\EQuickQuizEnd
%%
%% Copyright (C) IBM Corporation, 2008-2019
%% Copyright (C) Facebook, 2019
%% Copyright (C) Paolo Bonzini, 2015
%% Copyright (C) Akira Yokosawa, 2016, 2017, 2020
%%
%% Authors: Paul E. McKenney <paulmck@kernel.org>
%%          Paolo Bonzini <pbonzini@redhat.com> # cross-link questions and answers
%%          Akira Yokosawa <akiyks@gmail.com>   # improvement of cross-link

\tcbset{enhanced,colback=black!5,boxrule=0.5pt,top=1pt,bottom=1pt,left=0pt,right=0pt}
\newcounter{quickquizctr}[chapter]
\newcounter{quickquizctrC}[section]
\newcounter{quickquizctrP}[chapter] % for noqqz build page anchor
\newcommand*{\theHNum}{\arabic{chapter}.\arabic{quickquizctr}}
\newcommand{\QuickQuizAnswerChapter}{\textbf{Unknown QuickQAC!!!}}

\newcommand{\QuickQHeading}[3]{\hyperref[#2.#3]{\textbf{Quick Quiz #3:}}\label{#1.#3}}
\IfNoQqz{
\newcommand{\QuickQHeadingPage}[3]{\hyperref[#2.#3]{\textbf{Quick Quiz #3:}}\label{#1.#3}%
	\hfill\raisebox{0.5ex}{\footnotesize\fbox{p.\pageref{QQP.#3}}}}
}{\IfQqzBg{
\newcommand{\QuickQHeadingPage}[3]{\hyperref[#2.#3]{\textbf{Quick Quiz #3:}}\label{#1.#3}%
	\hfill\raisebox{0.5ex}{\footnotesize\fbox{p.\pageref{QQ.#3}}}}
}{% draft build
\newcommand{\QuickQHeadingPage}[3]{\hyperref[#2.#3]{\textbf{Quick Quiz #3:}}\label{#1.#3}%
	\hfill{\footnotesize{[p.\pageref{QQ.#3}]}}}
}}
\newcommand{\QuickQHeadingLight}[3]{\scriptsize QQ~\hyperref[#2.#3]{#3}\label{#1.#3}}
\newcommand{\QuickQHeadingBegin}[3]{\scriptsize QQ~\hyperref[#2.#3]{#3}\label{#1.#3},}
\newcommand{\QuickQHeadingMiddle}[3]{\scriptsize\hyperref[#2.#3]{{ #3}}\label{#1.#3},}
\newcommand{\QuickQHeadingEnd}[3]{\scriptsize\hyperref[#2.#3]{{ #3}}\label{#1.#3}}

\newcommand{\QuickQuizChapter}[3]{
	\chapter{#2}
	\label{#1}
	}
\IfNoQqz{
\newcommand{\QuickQuizSeries}[1]{%
	\vspace{-1pt}\hfill\fbox{#1}\vspace{1pt}}
\newcommand{\QuickQuiz}[1]{%
	\vspace{-1pt}%
	\hfill\fbox{\refstepcounter{quickquizctrP}%
	\QuickQHeadingLight{QQP}{QQ}{\thechapter.\thequickquizctrP}}%
	\vspace{1pt}}
\newcommand{\QuickQuizB}[1]{%
	\refstepcounter{quickquizctrP}%
	\QuickQHeadingBegin{QQP}{QQ}{\thechapter.\thequickquizctrP}}
\newcommand{\QuickQuizM}[1]{%
	\refstepcounter{quickquizctrP}%
	\QuickQHeadingMiddle{QQP}{QQ}{\thechapter.\thequickquizctrP}}
\newcommand{\QuickQuizE}[1]{%
	\refstepcounter{quickquizctrP}%
	\QuickQHeadingEnd{QQP}{QQ}{\thechapter.\thequickquizctrP}}
\newcommand{\EQuickQuiz}[1]{%
	\begin{tcolorbox}[breakable]
	\leavevmode\refstepcounter{quickquizctrP}%
	\small\QuickQHeading{QQP}{QQA}{\thechapter.\thequickquizctrP}%
	{#1}
}
\newcommand{\QuickQuizAnswer}[1]{\label{QQE.\thechapter.\thequickquizctrP}}
\newcommand{\QuickQuizAnswerB}[1]{\QuickQuizAnswer{#1}}
\newcommand{\QuickQuizAnswerM}[1]{\QuickQuizAnswer{#1}}
\newcommand{\QuickQuizAnswerE}[1]{\QuickQuizAnswer{#1}}
\newcommand{\EQuickQuizAnswer}[1]{%
	\hyperref[QQA.\thechapter.\thequickquizctrP]{\rule{7pt}{7pt}}%
	\phantomsection\label{QQE.\thechapter.\thequickquizctrP}%
	\end{tcolorbox}
}
\newcommand{\QuickQuizEnd}{\goodbreak}
\newcommand{\QuickQuizEndB}{}
\newcommand{\QuickQuizEndM}{}
\newcommand{\QuickQuizEndE}{\goodbreak}
\newcommand{\EQuickQuizEnd}{}
}{
\newcommand{\QuickQuizSeries}[1]{#1}
\IfQqzBg{
\newcommand{\QuickQuiz}[1]{
	\begin{tcolorbox}[breakable]
	\leavevmode\refstepcounter{quickquizctr}%
	\small\QuickQHeading{QQ}{QQA}{\thechapter.\thequickquizctr}
	{#1}
}
\newcommand{\QuickQuizAnswer}[1]{%
	\hyperref[QQA.\thechapter.\thequickquizctr]{\rule{7pt}{7pt}}%
	\phantomsection\label{QQE.\thechapter.\thequickquizctr}
	\end{tcolorbox}
}
\newcommand{\QuickQuizEnd}{}
}{
\newcommand{\QuickQuiz}[1]{
	\goodbreak\refstepcounter{quickquizctr}%
	\QuickQHeading{QQ}{QQA}{\thechapter.\thequickquizctr}
	{#1}}
\newcommand{\QuickQuizAnswer}[1]{%
	\hyperref[QQA.\thechapter.\thequickquizctr]{\rule{7pt}{7pt}}%
	\phantomsection\label{QQE.\thechapter.\thequickquizctr}}
\newcommand{\QuickQuizEnd}{\goodbreak}
}
\newcommand{\QuickQuizB}[1]{\QuickQuiz{#1}}
\newcommand{\QuickQuizM}[1]{\QuickQuiz{#1}}
\newcommand{\QuickQuizE}[1]{\QuickQuiz{#1}}
\newcommand{\EQuickQuiz}[1]{\QuickQuiz{#1}}
\newcommand{\QuickQuizAnswerB}[1]{\QuickQuizAnswer{#1}}
\newcommand{\QuickQuizAnswerM}[1]{\QuickQuizAnswer{#1}}
\newcommand{\QuickQuizAnswerE}[1]{\QuickQuizAnswer{#1}}
\newcommand{\EQuickQuizAnswer}[1]{\QuickQuizAnswer{#1}}
\newcommand{\QuickQuizEndB}{\QuickQuizEnd}
\newcommand{\QuickQuizEndM}{\QuickQuizEnd}
\newcommand{\QuickQuizEndE}{\QuickQuizEnd}
\newcommand{\EQuickQuizEnd}{\QuickQuizEnd}
}

% To create a macro referencing the previously defined quick quiz:
%	\QuickQuizLabel{\QQname}
%
% When labeling a QQz inside \QuickQuizSeries{}, use
%	\QuickQuizLabelRel{\QQname}{rel}
% in front of the series.
%
% To reference the macro in the text:
%	\QuickQuizRef{\QQname}
% To reference the answer of the macro in the text:
%	\QuickQuizARef{\QQname}
% The reference also generates a hyperlink.

\IfNoQqz{
\newcommand{\QuickQuizLabel}[1]{
	\edef#1{\thechapter.\thequickquizctrP}
}
\newcommand{\QuickQuizLabelRel}[2]{
        \addtocounter{quickquizctrP}{#2}
	\QuickQuizLabel{#1}
	\addtocounter{quickquizctrP}{-#2}
}
\newcommand{\QuickQuizRef}[1]{%
	\hyperref[QQ.#1]{Quick Quiz~#1}%
}
\newcommand{\QuickQuizARef}[1]{%
	\hyperref[QQA.#1]{Quick Quiz~#1}%
}
}{
\newcommand{\QuickQuizLabel}[1]{
	\edef#1{\thechapter.\thequickquizctr}
}
\newcommand{\QuickQuizLabelRel}[2]{
        \addtocounter{quickquizctr}{#2}
	\QuickQuizLabel{#1}
	\addtocounter{quickquizctr}{-#2}
}

\newcommand{\QuickQuizRef}[1]{%
	\hyperref[QQ.#1]{Quick Quiz~#1}%
}
\newcommand{\QuickQuizARef}[1]{%
	\hyperref[QQA.#1]{Quick Quiz~#1}%
}
}

\newcommand{\QuickQuizAnswers}{%
	\renewcommand*{\theHNum}{\arabic{section}.\arabic{quickquizctrC}}
	\chapter{Answers to Quick Quizzes}
	\label{chp:app:Answers to Quick Quizzes}
	\Epigraph{The Answer to the Ultimate Question of Life, The Universe,
		  and Everything.}
		 {\emph{The Hitchhikers Guide to the Galaxy}, Douglas~Adams}
	\setlength{\parskip}{0.0pt plus 1ex}
	\input{qqz}
	\setlength{\parskip}{0.0pt plus 1.0pt}% return to default
}
\IfNoQqz{
\newcommand{\QuickQuizAnswersChp}[1]{
	\setlength{\parskip}{0.0pt plus 1ex}
	\input{#1}
	\setlength{\parskip}{0.0pt plus 1.0pt}% return to default
}}{
\newcommand{\QuickQuizAnswersChp}[1]{}
}

%% Internal interfaces generated by scripts.

\newcommand{\QuickQAC}[3]{
	\pagebreak[3]
	\edef\QuickQuizAnswerChapter{\getrefnumber{#1}}
	\section{#2}\nopagebreak}

\IfNoQqz{
\newcommand{\QuickQQC}[3]{
	\edef\QuickQuizAnswerChapter{\getrefnumber{#1}}
	\section{Quick Quizzes}
	\label{sec:#3:Quick Quizzes}}
}{}

\IfQqzBg{
\newcommand{\QuickQ}[1]{
	\begin{tcolorbox}[breakable]
	\leavevmode\refstepcounter{quickquizctrC}%
	\QuickQHeadingPage{QQA}{QQ}{\QuickQuizAnswerChapter.\thequickquizctrC} \\
}

\newcommand{\QuickA}[1]{%
	\hyperref[QQ.\QuickQuizAnswerChapter.\thequickquizctrC]{\rule{7pt}{7pt}}
	\end{tcolorbox}
	\noindent\textbf{Answer:} \\ }
}{\IfNoQqz{
\newcommand{\QuickQQ}[1]{
	\begin{tcolorbox}[breakable]
	\leavevmode\refstepcounter{quickquizctr}%
	\QuickQHeadingPage{QQ}{QQA}{\thechapter.\thequickquizctr}\\
}

\newcommand{\QuickQE}[1]{%
	\hyperref[QQA.\thechapter.\thequickquizctr]{\rule{7pt}{7pt}}%
	\end{tcolorbox}
}

\newcommand{\QuickQ}[1]{
	\begin{tcolorbox}[breakable]
	\leavevmode\refstepcounter{quickquizctrC}%
	\QuickQHeadingPage{QQA}{QQ}{\QuickQuizAnswerChapter.\thequickquizctrC}\\
}

\newcommand{\QuickA}[1]{%
	\hyperref[QQ.\QuickQuizAnswerChapter.\thequickquizctrC]{\rule{7pt}{7pt}}
	\end{tcolorbox}
	\noindent\textbf{Answer:} \\ }
}{
\newcommand{\QuickQ}[1]{
	\par
	\refstepcounter{quickquizctrC}%
	\noindent
	\QuickQHeadingPage{QQA}{QQ}{\QuickQuizAnswerChapter.\thequickquizctrC}\\}

\newcommand{\QuickA}[1]{%
	\hyperref[QQ.\QuickQuizAnswerChapter.\thequickquizctrC]{\rule{7pt}{7pt}}
	\par\vspace{0.2\baselineskip plus .2ex minus .1ex}
	\noindent\textbf{Answer:} \\ }
}}

\newcommand{\QuickE}{%
	\hyperref[QQE.\QuickQuizAnswerChapter.\thequickquizctrC]{\ding{113}}
	\goodbreak\vspace{0.7\baselineskip plus .5ex minus .2ex}
}
