Is it possible for an environment to detect which sectioning level it is at?
I'm in the process of creating a package which defines a numbered environment of question/answer pairs, and I want to provide the option to customise which section level the numbers should be reset at, as well as whether the question/answer pairs should be included in the table of contents. The idea is for these Q/A pairs to form a parallel set of sectioning-like environments that won't interfere with the normal sectioning commands.
EDIT Perhaps a bit more context would be useful. I've been using a macro not unlike the one reproduced below to create question/answer environments in my assignments that would function similarly to sections, but have their own counters. This is such useful code that I'm trying to split it out into a package I can reuse, but I need to figure out a way to make the question
environment figure out whether it's inside a section in order to set its numbering correctly as well as optionally put it in the table of contents and PDF outline/index.
% vim: ft=tex
% arara: lualatex
% arara: lualatex
\documentclass{scrartcl}
\usepackage{etoolbox}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\newlength{\questionbeforeskip}
\newlength{\questionafterskip}
\setlength{\questionbeforeskip}{1.0ex plus -1ex minus -0.25ex}
\setlength{\questionafterskip}{1ex plus 0.25ex}
\newlength{\answerskip}
\setlength{\answerskip}{0.25ex plus -0.125ex minus -0.125ex}
\addtotoclist{loq}
\newcommand{\listofloqname}{List of Questions}
\newcommand{\listofquestions}{\listoftoc{loq}}
\setuptoc{loq}{leveldown}
\newcounter{question}[section]
\newcommand{\questionautorefname}{question}
\newenvironment{question}
{
\refstepcounter{question}%
\addxcontentsline{loq}{subsection}{Question~\thequestion}
\pdfbookmark[2]{Question~\thequestion}{question:\thesection.\thequestion}
{{\par\vspace\questionbeforeskip \large Question~\thequestion.}\par}
\begin{itshape}
}
{\end{itshape}\par\vspace\questionafterskip}
\usepackage[inline]{enumitem}
\setlist[enumerate]{label=(\alph*)}
% Sub-questions are represented as an enumerate
\newlist{subquestions}{enumerate}{3}
\setlist[subquestions]{leftmargin=*}
\setlist[subquestions,1]{label=\textup{\textbf{(\alph*)}}}
% Inline subquestions
\newlist{subquestions*}{enumerate*}{3}
\setlist[subquestions*]{itemjoin*={{, and }}}
\setlist[subquestions*,1]{label=\textup{\textbf{(\alph*)}}}
\newcommand{\qitem}[1]{\item{\textit{#1}}}
\newcommand{\qitemp}[1]{\qitem{#1}\\[0.5\parskip]}
\newcommand{\qitemq}[1]{\qitem{#1}\quad}
%% Penalties
\hyphenpenalty=2500
\tolerance=500
\widowpenalty=1500
\clubpenalty=1500
\usepackage{hyperref}
\hypersetup{
colorlinks=false,
hidelinks}
\usepackage[all]{hypcap}
\usepackage[noabbrev,capitalise]{cleveref}
\labelcrefformat{subfigure}{\textbf{(#2#1#3)}}
\begin{document}
\section{Osmosis and kinetics}
\begin{question}
What is reverse osmosis? Describe a process which involves the use of reverse
osmosis (use a diagram in your answer).
\end{question}
Answer blah
\begin{question}
What are colloidal particles? What are the main processes that are used to
remove colloidal particles from aqueous solutions?
\end{question}
Some other answer
\section{Organic chemistry / Natural gas processing}
\begin{question}
What are four ways of representing organic molecules such as alkanes?
\end{question}
\begin{question}
Draw structural formula for the following compounds:
\begin{subquestions}
\item 3-methylnonane
\item 3-ethylheptane
\item 2-methyloctane
\end{subquestions}
\end{question}
\begin{subquestions}
\item Answer 1
\item Answer 2
\item Answer 3
\end{subquestions}
\end{document}
So far what I've managed to extract into a package is the following. The PGFKeys switch to choose which section level to reset the question numbering is a bit clumsy, so I will probably replace it with something simpler (maybe xkeyval?). I'm using this exercise as an attempt to learn the ropes at creating a functional package that might be useful to someone other than myself. To use this package in the previous example, you'd have to load it with \usepackage{questions}
and remove the duplicated code.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{questions}
[2017/11/17 v0.1 Assignment Questions]
\RequirePackage{etoolbox}
\RequirePackage{pgfopts}
\RequirePackage{chngcntr}
\RequirePackage[inline]{enumitem}
% Question counter
\newcounter{question}
% Define option keys with PGFKeys
\pgfkeys{/questions/.cd}
\pgfkeys{/questions/reset/.cd,
.is choice,
part/.code={%
\counterwithin*{question}{part}
},
chapter/.code={%
\counterwithin*{question}{chapter}
},
section/.code={%
\counterwithin*{question}{section}
},
subsection/.code={%
\counterwithin*{question}{subsection}
},
subsubsection/.code={%
\counterwithin*{question}{subsubsection}
},
paragraph/.code={%
\counterwithin*{question}{paragraph}
},
subparagraph/.code={%
\counterwithin*{question}{subparagraph}
},
none/.code={%
\counterwithout*{question}{section}
},
}
% \pgfkeys{/questions/reset/.default = paragraph}
\newcommand{\questions@setdefaults}{
\pgfkeys{/questions/.cd,
reset=section,
}
}
\newcommand{\questionset}[1]{\pgfkeys{/questions/.cd,#1}}
\questions@setdefaults
\ProcessPgfOptions{/questions}
\newlength{\questionbeforeskip}
\newlength{\questionafterskip}
\setlength{\questionbeforeskip}{1.0ex plus -1ex minus -0.25ex}
\setlength{\questionafterskip}{1ex plus 0.25ex}
\newlength{\answerskip}
\setlength{\answerskip}{0.25ex plus -0.125ex minus -0.125ex}
\addtotoclist{loq}
% \newcommand{\listofloqname}{List of Questions}
% \newcommand{\listofquestions}{\listoftoc{loq}}
% \setuptoc{loq}{sectionatlist}
\newenvironment{questionenv}
{
\refstepcounter{question}%
% \addxcontentsline{loq}{subsection}{Question~\thequestion}
% \pdfbookmark{Question~\thequestion}{question:\thequestion}
{{\par\vspace\questionbeforeskip Question~\thequestion.}\par}
\begin{itshape}
}
{\end{itshape}\par\vspace\questionafterskip}
\newlist{subquestions}{enumerate}{3}
\setlist[subquestions]{leftmargin=*}
\setlist[subquestions,1]{label=\textup{\textbf{(\alph*)}}}
% Inline subquestions
\newlist{subquestions*}{enumerate*}{3}
\setlist[subquestions*]{itemjoin*={{, and }}}
\setlist[subquestions*,1]{label=\textup{\textbf{(\alph*)}}}
\endinput
The next MWE illustrates how the questions
package is intended to be used. Setting the restart
option changes where the questions are numbered from (it defaults to section
).
% vim: ft=tex
% arara: lualatex
% arara: lualatex
\documentclass{scrartcl}
%% KOMA options
\KOMAoption{cleardoublepage}{empty}
\KOMAoption{DIV}{10}
\KOMAoption{draft}{false}
\KOMAoption{fontsize}{10pt}
\KOMAoption{headings}{small}
\KOMAoption{paper}{a4}
\KOMAoption{parskip}{half}
\KOMAoption{twoside}{false}
\KOMAoption{captions}{signature}
\KOMAoption{titlepage}{false}
\KOMAoption{abstract}{false}
\usepackage{etoolbox}
\usepackage{fontspec}
\defaultfontfeatures{Ligatures=TeX}
\usepackage[reset=none]{questions}
%% Penalties
\hyphenpenalty=2500
\tolerance=500
\widowpenalty=1500
\clubpenalty=1500
\usepackage{hyperref}
\hypersetup{
colorlinks=false,
hidelinks}
\usepackage[all]{hypcap}
\usepackage[noabbrev,capitalise]{cleveref}
\labelcrefformat{subfigure}{\textbf{(#2#1#3)}}
\begin{document}
\section{A section}
\begin{questionenv}
The question in a section
\end{questionenv}
\subsection{A subsection}
\begin{questionenv}
The question in a subsection
\end{questionenv}
\begin{questionenv}
Another question in a subsection
\end{questionenv}
\subsubsection{A subsubsection}
\begin{questionenv}
The question in a subsubsection
\end{questionenv}
\begin{questionenv}
Another question in a subsubsection
\end{questionenv}
\subsubsection{A subsubsection}
\begin{questionenv}
The question in a subsubsection
\end{questionenv}
\begin{questionenv}
Another question in a subsubsection
\end{questionenv}
\subsection{Another subsection}
\begin{questionenv}
Another question in a subsection
\end{questionenv}
\begin{questionenv}
What is this question?
\end{questionenv}
\section{Another section}
\begin{questionenv}
Another question in another section
\end{questionenv}
\begin{questionenv}
Another question in another section
\end{questionenv}
\subsection{A subsection}
\begin{questionenv}
The question in a subsection
\end{questionenv}
\begin{questionenv}
Another question in a subsection
\end{questionenv}
\begin{questionenv}
Another question in a subsection
\end{questionenv}
\subsubsection{A subsubsection}
\begin{questionenv}
The question in a subsubsection
\end{questionenv}
\begin{questionenv}
Another question in a subsubsection
\end{questionenv}
\subsubsection{A subsubsection}
\begin{questionenv}
The question in a subsubsection
\end{questionenv}
\begin{questionenv}
Another question in a subsubsection
\end{questionenv}
\end{document}
chngcntr
and\counterwithin
in a PGF-keys.is choice
. What I'm stuck on is trying to set up a means to (optionally) add the Q/A pairs to the index / table of contents, such that they appear one level below their reset level.pgfkeys
but this is nowhere to be seen in your MWE.\numberwithin{question}{section}
, if you have loaded theamsmath
package, or\counterwithin{question}{section}
, with thechngcntr
package.