1

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:

Image showing "Z_p <newline> Theorem 1 (Z_n)"

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?

Bakuriu
  • 2,218
  • 2
    Optional argument inside an optional argument: it's a “quite frequently asked question”; the solution is indeed to brace the inner one. – egreg Sep 21 '15 at 08:13
  • 3
    http://tex.stackexchange.com/questions/99495/inside-an-optional-argument – BartekChom Sep 21 '15 at 08:25

0 Answers0