# Display environment differently for one use vs. two

My issue can be boiled down to this:

\documentclass[10pt,oneside]{article}
\usepackage{enumitem}

\newenvironment{mygroup}{
\vspace{.15in}

}{
}

\newenvironment{mylist}[1]{
\newcommand{\myitem}[1]{
\item ##1:%
}

\vspace{.12in}

\emph{\fontsize{11}{11}\selectfont#1}
\begin{itemize}[noitemsep,nolistsep]
}{
\end{itemize}
}

\begin{document}
\begin{mygroup}
\myitem{item 1} item 1 content
\myitem{item 2} item 2 content
\end{mylist}
\myitem{another item} content
\end{mylist}
\end{mygroup}
\end{document}


Currently, I display subheadings no matter how many mylists there are inside a mygroup. How do I make it so that subheadings only display if there are two or more mylists in a mygroup, and if there is only one, its subheading is not displayed?

• I'm confused by your question; if there are two mylists, subheadings would not be omitted. – Nate May 31 '15 at 16:25
• apologies. i misread your last paragraph. deleting comment. – barbara beeton May 31 '15 at 17:07

This uses my cntperchap package which stores total counters per chapter level, but this could be set to another counter as well. Here, the mygroup is set as tracklevel.

The \GetStoredCounterValue{mylist} gets the value from a previous run and stores it to cps@@tempstoragecounter counter, which can then be queried with \ifnumgreater (which is from etoolbox, but this package is loaded by cntperchap already.

(Side note: I have to change the documentation and the \GetStoredCounterValue approach in the next release -- it's not convenient so far.)

\documentclass[10pt,oneside]{article}
\usepackage{enumitem}

\newcounter{mygroup}
\usepackage[tracklevel=mygroup]{cntperchap}

\newcounter{mylist} % track the lists

\RegisterCounters{mylist} % prepare for tracking

\makeatletter

\newenvironment{mygroup}{%
\vspace{.15in}
}{
}

\makeatother

\newenvironment{mylist}[1]{%
\stepcounter{mylist}%
\GetStoredCounterValue{mylist}%
\newcommand{\myitem}[1]{%
\item ##1:%
}

\vspace{.12in}

\ifnumgreater{\value{cps@@tempcounterstorage}}{1}{%
\emph{\fontsize{11}{11}\selectfont#1}
}{}%

\begin{itemize}[noitemsep,nolistsep]
}{%
\end{itemize}
}

\begin{document}
\begin{mygroup}
\myitem{item 1} item 1 content
\myitem{item 2} item 2 content
\end{mylist}
\myitem{another item} content
\end{mylist}
\end{mygroup}

\begin{mygroup}
\myitem{item 1} Just one list, part 1
\myitem{item 2} Just one list, part 2
\end{mylist}
\end{mygroup}

\end{document}


• Alright, I have been struggling to get this installed, but I cannot get it to work, unfortunately. I tried digging through the code, and I am having trouble following it... my TeX just isn't good enough, yet. Would you be willing to add another answer that boils down your solution to the most basic thing that would work without installing cntperchap? – Nate May 31 '15 at 20:07
• @Nate: You can just put cntperchap.sty in your working directory. It was some hard work to make cntperchap work in general, but the effort for a 'small' problem like yours is effectively the same, so it would mean reinventing the wheel again. I can try, but can't promise to succeed. – user31729 May 31 '15 at 21:20
• I believe you... sadly, I think I may be working with an older version that doesn't support cntperchap. I tried following your suggestion to copy it into my working directory, but it gave me errors. If this is so complicated, I may just have to give up. – Nate May 31 '15 at 21:51
• @Nate: Apparently, you've got an older TeX distribution then. I will try another approach, but this might take some time – user31729 May 31 '15 at 21:52
• Actually, I came up with another approach. Thanks for all your help! I believe this is a better answer so I will to accept yours, even though I couldn't use it myself. – Nate Jun 1 '15 at 0:00

After meditating on Christian's answer and doing a bit more googling, I came up with a solution that uses the totcount package instead:

\documentclass[10pt,oneside]{article}
\usepackage{enumitem}
\usepackage{totcount}

\newcounter{mygroupcount}

\newenvironment{mygroup}{
\stepcounter{mygroupcount}
\edef\mycount{mycount\alph{mygroupcount}}
\expandafter\newtotcounter\expandafter{\mycount}
\vspace{.15in}

}{
}

\newenvironment{mylist}[1]{
\stepcounter{\mycount}
\newcommand{\myitem}[1]{
\item ##1:%
}

\vspace{.12in}

\ifnum 1<\totvalue{\mycount}
\emph{\fontsize{11}{11}\selectfont#1}
\fi
\begin{itemize}[noitemsep,nolistsep]
}{
\end{itemize}
}

\begin{document}
\begin{mygroup}
\myitem{item 1} item 1 content
\myitem{item 2} item 2 content
\end{mylist}
\myitem{another item} content
\end{mylist}
\end{mygroup}
\begin{mygroup}

• Thanks for accepting. Basically you are doing something similar what I've done, but \newtotcounter each time mygroup is used, isn't the best idea. It will work, as long as there no severe counter registers manipulations occur (I assume, that you won't do this anyway) (+1) – user31729 Jun 1 '15 at 0:11