Expanding on the answer here. I have a custom environment defined that automatically prepends the chapter number to its own counter. Obviously this only works for those documentclasses that define a chapter number and produces an error with article etc.
Is there a way to build an if/then statement into the environment itself that prefixes the chapter number only when chapternumber is defined and skips the prefix when not?
%\documentclass{article}
\documentclass{report}
\usepackage{xkeyval}
\usepackage{noindentafter}
\makeatletter
\define@boolkey{fam}[myenv@]{title}[true]{}
\define@boolkey{fam}[myenv@]{notitle}[true]{\ifmyenv@notitle\myenv@titlefalse\else\myenv@titletrue\fi}
\define@cmdkey{fam}[myenv@]{label}[\relax]{}
\define@cmdkey{fam}[myenv@]{repeat}[\relax]{}
\newcounter{myenvcnt}[chapter]%
\renewcommand{\themyenvcnt}{\thechapter.\arabic{myenvcnt}}
\newenvironment{myenv}[1][]{%
\setkeys{fam}{title,label,repeat,#1}%
\ifmyenv@title%
\par%
\expandafter\ifx\myenv@repeat\relax
\refstepcounter{myenvcnt}%
\expandafter\ifx\myenv@label\relax\else\label{\myenv@label}\fi
\else
\renewcommand{\themyenvcnt}{\ref{\myenv@repeat}}%
\fi
\subsubsection*{\textit{myenv\ \themyenvcnt}}%
\par\nobreak%
\else% = if no title
\par\addvspace{\bigskipamount}%
\fi
\edef\@currentlabel{\themyenvcnt}%
}{\par\addvspace{\bigskipamount}%
}
\NoIndentAfterEnv{myenv}
\appto\@noindent@newenv@hook{\par}{}{}
\makeatother
\begin{document}
\begin{myenv}
My environment.
\end{myenv}
\myenv{Test}
\end{document}
Any help how to do this or where to start would be much appreciated.
\@ifundefined{cmd}{then}{else}– nox Jul 10 '18 at 00:28