# How to adjust a counter depending on whether or not we are inside a section or subsection

I want to create an exercise environment that can be used in either section, subsection, etc. As an illustration, consider the following:

• when in section 1, the third example should be titled as Example 1.3, but
• when in section 1 and subsection 2, the third example should be titled as Example 1.2.3.

How to do this?

\documentclass{article}
\usepackage{tcolorbox}

\newcounter{exercise}
\newenvironment{exercise}
{\par\smallskip\refstepcounter{exercise}\begin{tcolorbox}[title=Exercise \thesection.\thesubsection.\theexercise]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}

\begin{document}
\section{Polynomial}
In this section we will study expressions in the following form.
$a_0 x^n + a_1 x^{n-1} \cdots + a_n$
where \ldots
\begin{exercise}
Is $\frac{x-1}{2}$ a polynomial?
\tcblower
Yes. Because it can be rewritten as $x/2-1/2$.
\end{exercise}
\begin{exercise}
Is $0$ a polynomial?
\tcblower
Yes.
\end{exercise}
\subsection{Order}
The highest exponent in polynomial terms represents the order of  the polynomial.
\begin{exercise}
What is the order of $2x^3-3x^5+1$?
\tcblower
The highest exponent is 5 so the polynomial order is 5.
\end{exercise}
\end{document}


I did as follows but the output has double subsection when I am in subsection.

\newcounter{exercise}
\newenvironment{exercise}
{\par\smallskip\refstepcounter{exercise}\begin{tcolorbox}[title=Exercise \thesection\ifnum\value{subsection}>0.\thesubsection\fi.\theexercise]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}


## Note:

Best practice suggestions are welcome to make the environment more and more sophisticated such as it can be referenced by label, etc, etc...

You want to put the prefix in \theexercise so it is seen by \ref not just in the title.

\documentclass{article}
\usepackage{tcolorbox}

\newcounter{exercise}[subsection]
\renewcommand\theexercise{%
\thesection.%
\ifnum\value{subsection}>0 \arabic{subsection}.\fi
\arabic{exercise}}

\newenvironment{exercise}
{\par\smallskip\refstepcounter{exercise}
\begin{tcolorbox}[title=Exercise \theexercise]\ignorespaces}
{\end{tcolorbox}\par\smallskip\ignorespacesafterend}

\begin{document}
\section{Polynomial}
In this section we will study expressions in the following form.
$a_0 x^n + a_1 x^{n-1} \cdots + a_n$
where \ldots
\begin{exercise}
Is $\frac{x-1}{2}$ a polynomial?
\tcblower
Yes. Because it can be rewritten as $x/2-1/2$.
\end{exercise}
\begin{exercise}
Is $0$ a polynomial?
\tcblower
Yes.
\end{exercise}
\subsection{Order}
The highest exponent in polynomial terms represents the order of  the polynomial.
\begin{exercise}
What is the order of $2x^3-3x^5+1$?
\tcblower
The highest exponent is 5 so the polynomial order is 5.
\end{exercise}
\end{document}


Refinement for David Carlisle's solution, it is too long for a comment:

\newcounter{exercise}[subsection]
\makeatletter
\makeatother

\renewcommand\theexercise{%
\ifnum\value{subsection}>0 %
\thesubsection
\else
\thesection
\fi
.\arabic{exercise}%
}


Remarks:

• \newcounter{exercise}[subsection] only resets the counter for new \subsections, not for \section. An additional \@addtoreset{exercise}{section} is needed.

• Inside \theexercise I have used \thesubsection instead of \arabic{subsection}, then a change in \thesubsection is automatically reflected in \theexercise, e.g. if someone wants to have a different numbering style for subsections than the default.

I think you can compare the subsection counter to zero (e.g. using commands from the etoolbox) and use this to decide how to compose the number.