# Howto custom figure with counter w/o using figure-environment

I am trying to create a visually more appealing figure, that looks like the output of the MWE below.

\documentclass[a4paper]{book}

\usepackage[a4paper]{geometry}
\usepackage{kantlipsum}
\usepackage{tikz}
\usepackage{caption}

\newcommand{\CustomFigure}[3][.4\textwidth]
{
\stepcounter{figure}
%\captionof{figure}[The caption.]{}
\begin{tikzpicture}
\draw (0,0) node(origin) {};
%rectangle (.4\textwidth,40mm);
\draw (origin.west) +(4pt,-8pt) node(caporg){};
\draw [blue, fill=blue] (caporg.south) +(0.0,0.0) -- +(0.09,0.18) -- +(0.18,0) node (arroweast){} -- cycle;
\draw [font=\sffamily\bfseries,blue] (arroweast.east) +(-2pt,+10pt) node[anchor=north west](caption) {　Figure \thefigure};
\draw [font=\sffamily,black,text justified, text width=#1] (caporg.south west) +(-1pt,-2pt) node[anchor=north west](description)
{
\mdseries #3
};
\node[anchor=south west,inner sep=0pt] (image) (origin.north west)
{\includegraphics[width=.4\textwidth]{#2}};
\label{fig:NFb}
\end{tikzpicture}

} % \CustomFigure ENDS

\begin{document}

\kant[7]
Some more lipsum.\newline

\begin{figure}
\begin{center}
\includegraphics[width=.4\textwidth]{example-image-a};
\caption{Just a figure.}
\label{fig:NFa}
\end{center}
\end{figure}

\CustomFigure{example-image-b}{\textbf{Header text.} The crazy monkey jumped over the sleeping lion. He met quick brown fox from his own lipsum.}

See figures '\ref{fig:NFa}' and '\ref{fig:NFb}'  for details.

\newpage
\listoffigures

\end{document}


'example-image-a' is a figure as usual. Works of course.

Now the output of 'CustomImage' produces the desired visual outcome, but the list of figures is screwed up. It only shows the 'real' figure but not the custom-figure. (Btw. CustomFigure is not an environment because it would not draw more than a single char of the text.)

I though this could be solved by adding a \captionof{figure}[The caption.]{} (which it does indeed) but then there would also be the caption itself placed as well. Is there a way to JUST set the caption for \thefigure (the figure stepped with stepcounter) invisibly, without the need to use captionof?

The list of figures must show the content of the 'hidden' caption of course.

Or do I need to re-implement \listoffigures myself?

• Try \captionlistentry{figure}{Something}. – Johannes_B Nov 30 '17 at 6:45
• @Johannes_B doesn't work with the MWE. (Undefined control sequence). Do I need to add some additional packages? – Edward Nov 30 '17 at 6:48
• you also need \refstepcounter not \stepcounter if you want \label to work. – David Carlisle Nov 30 '17 at 9:06
• @David Yes, thanks. That solved one error. But the 'List of Figures' still wont show and figures. I guess because no captions are defined for them, right? – Edward Nov 30 '17 at 9:22
• @Edward: Since neither \caption nor \captionof is used/wanted, nothing writes information to the .lof file. You have to add it manually with \addcontentsline{lof}{figure}{\protect\numberline{\thefigure~}Your caption text}, where Your caption text seems to be #3 here – user31729 Nov 30 '17 at 9:30

Since neither \caption nor \captionof are wanted/used, no command writes something to the .lof file -- \CustomFigure will not make entries to the .lof file.

Those have to be done manually with \addcontentsline{lof}{figure}{\protect\numberline{\thefigure}#4} where #4 is the caption text which is from an optional 4th argument which defaults to #3 if it is not specified. (see the 3rd usage).

\refstepcounter usage has been suggested by David Carlisle already.

\documentclass[a4paper]{book}

\usepackage[a4paper]{geometry}
\usepackage{kantlipsum}
\usepackage{tikz}

\usepackage{xparse}
\usepackage{caption}

\NewDocumentCommand{\CustomFigure}{O{.4\textwidth}+m+m+O{#3}}{%
\refstepcounter{figure}
%\captionof{figure}[The caption.]{}
\begin{tikzpicture}
\draw (0,0) node(origin) {};
%rectangle (.4\textwidth,40mm);
\draw (origin.west) +(4pt,-8pt) node(caporg){};
\draw [blue, fill=blue] (caporg.south) +(0.0,0.0) -- +(0.09,0.18) -- +(0.18,0) node (arroweast){} -- cycle;
\draw [font=\sffamily\bfseries,blue] (arroweast.east) +(-2pt,+10pt) node[anchor=north west](caption) {　Figure \thefigure};
\draw [font=\sffamily,black,text justified, text width=#1] (caporg.south west) +(-1pt,-2pt) node[anchor=north west](description)
{
\mdseries #3%
};
\node[anchor=south west,inner sep=0pt] (image) (origin.north west)
{\includegraphics[width=.4\textwidth]{#2}};
\end{tikzpicture}
} % \CustomFigure ENDS

\begin{document}

\kant[7]
Some more lipsum.\newline

\begin{figure}
\begin{center}
\includegraphics[width=.4\textwidth]{example-image-a};
\caption{Just a figure.}
\label{fig:NFa}
\end{center}
\end{figure}

\CustomFigure{example-image-b}{\label{fig:NFb}\textbf{Header text.} The crazy monkey jumped over the sleeping lion. He met quick brown fox from his own lipsum. }

\CustomFigure{example-image-b}{\label{fig:NFc}\textbf{Header text.} The crazy monkey jumped over the sleeping lion. He met quick brown fox from his own lipsum.}[And some shorter Text]

See figures '\ref{fig:NFa}' and '\ref{fig:NFb}'  for details.

\listoffigures

\end{document}


Version without \NewDocumentCommand

\documentclass[a4paper]{book}

\usepackage[a4paper]{geometry}
\usepackage{kantlipsum}
\usepackage{tikz}

\usepackage{caption}

\newcommand{\CustomFigure}[3][0.4\textwidth]{%
\refstepcounter{figure}
\begin{tikzpicture}
\draw (0,0) node(origin) {};
%rectangle (.4\textwidth,40mm);
\draw (origin.west) +(4pt,-8pt) node(caporg){};
\draw [blue, fill=blue] (caporg.south) +(0.0,0.0) -- +(0.09,0.18) -- +(0.18,0) node (arroweast){} -- cycle;
\draw [font=\sffamily\bfseries,blue] (arroweast.east) +(-2pt,+10pt) node[anchor=north west](caption) {　Figure \thefigure};
\draw [font=\sffamily,black,text justified, text width=#1] (caporg.south west) +(-1pt,-2pt) node[anchor=north west](description)
{
\mdseries #3%
};
\node[anchor=south west,inner sep=0pt] (image) (origin.north west)
{\includegraphics[width=.4\textwidth]{#2}};
\end{tikzpicture}
} % \CustomFigure ENDS

\begin{document}

\kant[7]
Some more lipsum.\newline

\begin{figure}
\begin{center}
\includegraphics[width=.4\textwidth]{example-image-a};
\caption{Just a figure.}
\label{fig:NFa}
\end{center}
\end{figure}

\CustomFigure{example-image-b}{\label{fig:NFb}\textbf{Header text.} The crazy monkey jumped over the sleeping lion. He met quick brown fox from his own lipsum. }

\CustomFigure{example-image-b}{\label{fig:NFc}\textbf{Header text.} The crazy monkey jumped over the sleeping lion. He met quick brown fox from his own lipsum.}

See figures '\ref{fig:NFa}' and '\ref{fig:NFb}'  for details.

\listoffigures

\end{document}

• This MWE does not compile with y MikTex, "Illegal parameter number in definition of _xparse_grab_arg" – Edward Nov 30 '17 at 9:47
• @Edward: It compiles for me out of the box – user31729 Nov 30 '17 at 9:48
• I do believe you :-) Maybe somethings' wrong with my latex. Wouldn't be the first time. Care to provide an alternative solution NOT based on xparse? – Edward Nov 30 '17 at 9:51
• @Edward: Yes, but you will lose the optional 4th argument then – user31729 Nov 30 '17 at 9:52
• Ahh, yes. That problem. I already got used to it. I would keep your first solution (xparse) for future reference and the usual \newcommand solution as default/fallback. – Edward Nov 30 '17 at 9:54