# Normalizing nested environments for cleaner design (LyX)

LyX supports nested environments (e.g., begin{itemize}...\item...) intuitively with the Tab key. I'd like to get this to work for questions in a layout with the Exam class.

Exam nests questions with the following itemized-like environments: Questions, Parts, Subparts, Subsubparts. The trouble with this in LyX is that a tab key in LyX recreates (nests) the same environment. A user has to both use the Tab key (to change the nesting level) and change the paragraph style (e.g., Question -> Part after a tab press).

Since LyX is set up to nest intuitively if the environment names are the same, I thought of the idea to code only one paragraph style in a layout, such as the myquestions environment with an \item that is \myquestion

LyX can easily create a LaTeX file with:

\begin{myquestions}
\myquestion[4] What is 2 + 2?
\myquestion Answer each of the following as best you can.
\begin{myquestions}
\myquestion[2] What's the meaning of life.
\myquestion[2] How old are you.
\end{myquestions}
\end{myquestions}


The problem I'm having is how do I get the above to convert to what Exam expects. That is:

\begin{questions}
\question[4] What is 2 + 2?
\question Answer each of the following as best you can.
\begin{parts}
\part[2] What's the meaning of life.
\part[2] How old are you.
\end{parts}
\end{questions}


I can manage to re-write the \myquestion to the proper \question or \part item depending on current environment (sort of, because it always seems to be the same, e.g., myquestions). It seems that coding this requires knowing what level of nesting one is in. ​Command behavior depending on current environment seems useful, for example.

The second problem is where I'm totally stumped: how to convert an environment name (\begin{myquestions} -> \begin{questions} or \begin{parts} depending on the nesting level). It's out of my league for coding LaTeX and I didn't find anything on TSE.

Edit: thanks to the help here, I've got my LyX Exam document class layout working (and documented).

• Since you describe how to convert your LyX challenge into a LaTeX challenge, I believe that this is a general LaTeX question rather than a LyX-specific question. I hope that LaTeX gurus can help you with this.
– G.M.
Jul 18, 2016 at 18:06

Here's my naive shot:

\documentclass{exam}
\newcounter{mydepth}%
\newcommand{\myenvironmentname}{%
\ifnum\value{mydepth}=0notinquestion\fi%
\ifnum\value{mydepth}=1question\fi%
\ifnum\value{mydepth}=2part\fi%
\ifnum\value{mydepth}=3subpart\fi%
\ifnum\value{mydepth}=4subsubpart\fi%
\ifnum\value{mydepth}>4undefinedquestion\fi%
}%
\newcommand{\myquestion}{\csname \myenvironmentname\endcsname}%
\newenvironment{myquestions}%
{\stepcounter{mydepth}\begin{\myenvironmentname s}}%

\begin{document}
\begin{myquestions}
\myquestion[4] What is 2 + 2?
\myquestion Answer each of the following as best you can.
\begin{myquestions}
\myquestion[2] What's the meaning of life.
\myquestion[2] How old are you.
\end{myquestions}
\end{myquestions}
\end{document}

• It works wonderfully well when I updated the LyX style in my layout, too. Thank you very much! Many redundant styles disappear. Truly Occam's razor. Jul 19, 2016 at 2:10
• Let's hope it does... there might be corner cases
– G.M.
Jul 19, 2016 at 10:54
• I put the layout file and sample exam on GitHub: github.com/fuhrmanator/lyx-layouts Jul 23, 2016 at 2:22