Consider the following latex code:
\documentclass{article}
\usepackage{amssymb}
\usepackage{amsthm}
\newtheorem{thm}{Theorem}
\newcommand{\Zn}[1][n]{\mathbb{Z}_{#1}}
\begin{document}
$\Zn[p]$
\begin{thm}[$\Zn$]
\end{thm}
\end{document}
This compiles without errors obtaining:
Now, if I change the \Zn to \Zn[p] inside the theorem title:
\begin{thm}[$\Zn[p]$]
\end{thm}
I get the following error when compiling:
! Argument of \\Zn has an extra }.
<inserted text>
\par
l.14 \begin{thm}[$\Zn[p]
$]
Runaway argument?
p)
! Paragraph ended before \\Zn was complete.
Yet the \Zn[p] outside the title works fine. Moreover if I put the command in a group, as in {\Zn[p]}, it compiles successfully with the expected output:
\begin{thm}[${\Zn[p]}$]
\end{thm}
Is there something wrong with my definition of \Zn? Or is there a bug in how the theorem environment handles its title? Why does this happen?
