[Tex/LaTex] Putting a matrix inside of caption gives \caption has an extra }

captionserrorsgraphics

I am trying to insert a matrix inside of the caption of an image.

enter image description here

The compiled pdf displays the picture, but gives an error that says:

\caption@{indecipherable} has an extra }

enter image description here

Can someone please point to me where I might have inserted an extra }? Because I don't see it anywhere!

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}       
\usepackage{graphicx}
\setlength\parindent{0pt}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{hyperref}
\usepackage{caption} 
\usepackage{cleveref}

\begin{document}
\section{Introduction}
\begin{figure}[ht]
    \begin{center}
        \includegraphics{jpg-to-pdf.pdf}
    \end{center}
    \caption{$x^\star =\begin{bmatrix} \alpha, \beta, \gamma \end{bmatrix}$}
    \label{fig:just_picture_of_cute_cat}
\end{figure}
\end{document}

Does anyone also know what ydblarg is?

Best Answer

\begin{matrix} and \end{matrix} are fragile commands that do not survive the writing process being connected to \caption -- the content is written to the .aux file and to the .lof file later on with \@writefile.

In order to prevent the fragility there, the commands \begin{matrix} and \end{matrix} have to be protected with \protect, i.e. \protect\begin{matrix} and \protect\end{matrix}.

If the protection is not activated, \caption prematurely encounters a } which is not the ending bracket of its mandatory argument.

A better way is to apply the optional argument of \caption and prevent the writing of math content to the .aux and .lof file, i.e. use the short and the long caption style.

However, if fragile content should be written in the short caption argument, the protection must be enabled again.

Alternatively use robust commands.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}       
\usepackage{graphicx}
\setlength\parindent{0pt}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{hyperref}
\usepackage{caption} 
\usepackage{cleveref}

\begin{document}
\listoffigures
\section{Introduction}
\begin{figure}[ht]
  \centering

  \includegraphics{ente}
  \caption{$x^\star =\protect\begin{bmatrix} \alpha, \beta, \gamma \protect\end{bmatrix}$}
  \caption[Foo content]{$x^\star =\begin{bmatrix} \alpha, \beta, \gamma \end{bmatrix}$}
  \label{fig:just_picture_of_cute_cat}
\end{figure}
\end{document}

enter image description here

With a cat :-P

enter image description here