# Why does caption throw an error?

I used the solution from here which works kind of but throws an error:

pdflatex> ! Argument of \@caption has an extra }.
...
pdflatex> ! Paragraph ended brfore \@caption was complete.

Here is a reproducible example - If you load any picture, there are still the two errors above:

\documentclass[12pt]{article}

\usepackage{a4wide,german,fancyhdr}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{epstopdf}

\begin{document}

text before

\begin{figure}[h!]
\centering
%\includegraphics[width=0.5\textwidth]{path2/figure.png}
\caption{first line\\\hspace{\textwidth} Second line.}
\end{figure}

text after

\end{document}

• Please, note that german.sty is obsolete; do \usepackage[ngerman]{babel} instead. The a4wide package has been deprecated for several years. – egreg Oct 15 '18 at 11:39

\\ is sort of a fragile command. When written to the aux file it is expanded multiple times, resulting in an error. Prefix \\ with \protect and the error goes away. Even better though, don't use line breaks in the caption, it doesn't look nice.

\documentclass{article}
\usepackage{graphicx}

\begin{document}

text before

\begin{figure}[h!]
\centering
\includegraphics[width=0.5\textwidth]{example-image-a}
\caption{first line\protect\\\hspace{\textwidth} Second line.}
\end{figure}

text after

\end{document}

• Good point with extra line. But I would like to include a reference "http.//..." and this is more than one line... – Christoph Oct 15 '18 at 11:49