8

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?

  • 1
    Oh my a cute cat ;-) .. But try \protect\begin{bmatrix}....\protect\end{bmatrix}, and remove the \begin{center}...\end{center} there, it does nothing good there – user31729 Apr 16 '17 at 18:12
  • @ChristianHupfer Hi, is there like a one-line self-explanatory answer as to why \protect is necessary? – Carlos - the Mongoose - Danger Apr 16 '17 at 18:14
  • \begin{matrix} is a fragile command, i.e. it breaks during the write process to the .aux/.lof, so this breaking is prevented with \protect – user31729 Apr 16 '17 at 18:16
  • @ChristianHupfer Never heard of such thing as a fragile command, but this is good enough for me! – Carlos - the Mongoose - Danger Apr 16 '17 at 18:18
  • @ChristianHupfer: I'm surprised! Why is \matrix a fragile command? I thought that fragile commands were usually commands like \footnote or \figure, where the arguments might need to be saved and typeset later or in a different place. Though…now that I think about it, \lstinline is fragile and doesn't fit this pattern, either; extra kudos if you know that one, too. :-) – wchargin Apr 16 '17 at 23:45
12

\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

  • 2
    Wow, magic! A cat turned into a duck! – user36296 Apr 16 '17 at 18:23
  • 2
    @samcarter: A Harry-Potter cat ;-) – user31729 Apr 16 '17 at 18:27
  • But you see, I want my picture to remain a cat in a pair of trousers, if \protect necessarily converts the image to a duck, then it is better to look for some workaround instead. Any suggestions? – Carlos - the Mongoose - Danger Apr 16 '17 at 18:58
  • 1
    You have a typo in your answer: \protect\end{protect} should be \protect\end{matrix} :) – sodd Apr 16 '17 at 20:53
  • 1
    @hooy: Yes, thanks. I've corrected it. It was correct in the code, however. – user31729 Apr 16 '17 at 20:54
5

Add \protect on entering and leaving the environment:

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

\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{graphicx}
\setlength\parindent{0pt}
\usepackage{caption}
\usepackage{hyperref}
\usepackage{cleveref}

\begin{document}

\begin{figure}[ht]
\centering
        \includegraphics[scale=0.5]{vent_cat}
    \caption{$x^\star =\protect\begin{bmatrix} \alpha, \beta, \gamma \protect\end{bmatrix}$}
    \label{fig:just_picture_of_cute_cat}
\end{figure}

\end{document} 

enter image description here

  • 1
    @Christian Hupfer: Yours is quite comprehensive. I guess that while you were writing details, I was looking for a relevant ‘cute cat’ from TeX Avery's gallery. I can withdraw my answer since it doesn't add whatever to yours, technically speaking. – Bernard Apr 16 '17 at 21:08
  • No, keep it please, I just wanted to know whether I was missing something. – user31729 Apr 16 '17 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.