# Error: Argument of \newcommand{…} has an extra }

I am trying to create a new command to help formatting the entries of the list of figures. Argument 1 should be optional and contain the date the source was accessed, argument 2 is simply the figure's caption I want to show up in the list of figures, and argument 3 is a hyperlink to the source of the image. I want the source and date to only show up in the list of figures and not underneath the actual image. I wrote this in the preamble:

\newcommand{\lof}[3][]{
\textbf{#2}
\linebreak
{\scriptsize #3 #1}
}


This is the caption of some figure:

\caption[
\lof[(Accessed on 02.01.2019)]
{Basic neural network with three layers}
{\url{https://commons.wikimedia.org/wiki/File:Artificial_neural_network.svg}}
]
{Basic neural network with three layers}


Note that I want to be able to declare that the image is my own, in which case the hyperlink and the date should be omitted.

\caption[
{Basic neural network with three layers}
]
{Basic neural network with three layers}


This gives me the following error:

! Argument of \\lof has an extra }.


However, the brackets should be balanced if I am not completely mistaken. I have read somewhere that there are 'fragile' commands, and using them leads to the same error message I got. Might \url{} be a fragile command? Or have I messed up the command definition somehow?

Edit:

The following code shows everything that might be related to the problem. If I remove these parts from the code, the LaTeX documents compiles.

\documentclass{article}
\usepackage{wrapfig,graphicx,amsmath,hyperref}

\newcommand{\lof}[3][]{\textbf{#2}\newline {\scriptsize #3 #1}}
\begin{document}

\listoffigures

\begin{wrapfigure}{r}{0.25\textwidth}
\centering
\includegraphics[width=0.25\textwidth]{example-image}
\caption[ {\lof[(Accessed on 02.01.2019)]
{Basic neural network with three layers}
{\url{https://commons.wikimedia.org/wiki/File:Artificial_neural_network.svg}}} ]
{Basic neural network with three layers}
\label{fig:nn}
\end{wrapfigure}

\begin{wrapfigure}{h}{0.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\caption[{\lof{$\boldsymbol{\sigma (x)}$, the sigmoid function}
{Created by the author}}]
{$\sigma (x)$, the sigmoid function}
\label{fig:sigmoid}
\end{wrapfigure}

\end{document}

• \caption[ {\lof[(Accessed on 02.01.2019)] {Basic neural network with three layers} {\url{https://commons.wikimedia.org/wiki/File:Artificial_neural_network.svg}}} ] {Basic neural network with three layers} -- you must enclose the \lot[...]{}{} command with a {} pair, since it has an optional argument and occurs in the optional argument of \caption – user31729 Jan 2 '19 at 22:26
• You might want to have a look at the xparse package, which makes it considerably easier to define commands with multiple optonal arguments. – GuM Jan 2 '19 at 22:33
• @Terrenay: Then add the complete code, not just fragments only – user31729 Jan 2 '19 at 22:36
• As you are new, I extended your fragment with the missing definitions and replacing with generally available images, however it does not produce the error that you are asking about. Please edit the example so that it is an example of the error. – David Carlisle Jan 2 '19 at 22:58
• also you should delete your .aux and .lof files which may be corrupted by previous errors. – David Carlisle Jan 2 '19 at 23:02

I get no error, but your input is very awkward and error prone.

I suggest to define a new \xcaption command, so as to minimize the burden.

\documentclass{article}
\usepackage{xparse}
\usepackage{amsmath}
\usepackage{wrapfig}
\usepackage{graphicx}
\usepackage{hyperref}

\usepackage{kantlipsum}

\NewDocumentCommand{\xcaption}{O{#2}mmo}{%
\caption[\lof{#1}{#3\IfValueT{#4}{ #4}}]{#2}%
}
\NewDocumentCommand{\lof}{mm}{%
{\boldmath\textbf{#1}}\newline
{\scriptsize #2}%
}

\begin{document}

\listoffigures

\begin{wrapfigure}{r}{0.25\textwidth}
\centering
\includegraphics[width=0.25\textwidth]{example-image}
\xcaption
{Basic neural network with three layers}
{\url{https://commons.wikimedia.org/wiki/File:Artificial_neural_network.svg}}
[(Accessed on 02.01.2019)]
\label{fig:nn}
\end{wrapfigure}

\kant[1]

\begin{wrapfigure}{h}{0.5\textwidth}
\centering
\includegraphics[width=0.5\textwidth]{example-image}
\xcaption
[Short caption with $\sigma(x)$]
{$\sigma (x)$, the sigmoid function}
{Created by the author}
\label{fig:sigmoid}
\end{wrapfigure}

\kant[1]

\end{document}


The syntax is

\xcaption
[<optional caption for the lof>]
{<main caption>}

I added an example of the optional caption, also showing how to get bold math without explicitly adding \boldsymbol.
The \lof command is only auxiliary and it shouldn't be really used in the document.