Wrapping the latex code for fast reusability

Dear Latex Community,

due to the help of Ulrike in my question How to get long captions across pages I can now finally use extra long captions for my theses. Thanks again :-)

I am looking for an easy way to repeat the suggested lines of code without copy/past it over the whole document multiple times. That is how the code looks like if I do it once manually:

\begin{figure}[H]
\includegraphics{largeImage}
\end{figure}
\bigskip
\setbox0\vbox{\makeatletter
\let\caption@rule\relax
\captionof{figure}[short caption]{\kant[1-4]}
\global\skip1\lastskip\unskip
\global\setbox1\lastbox
}
\unvbox0
\setbox0\hbox{\unhbox1\unskip\unskip\unpenalty
\global\setbox1\lastbox}
\unvbox1
\vskip\skip1


Instead I tried to create my own command like \longcaptions wo wrap this from above like this:

\newcommand{\longcaption}[1]{
\bigskip
\setbox0\vbox{\makeatletter
\let\caption@rule\relax
\captionof{figure}[short caption]{#1}
\global\skip1\lastskip\unskip
\global\setbox1\lastbox
}
\unvbox0
\setbox0\hbox{\unhbox1\unskip\unskip\unpenalty
\global\setbox1\lastbox}
\unvbox1
\vskip\skip1
}


and use it like this now:

\begin{figure}[H]
\includegraphics{largeImage}
\end{figure}
\longcaption{long long text}


unfortunately that results in no caption but simply writes under the image:

rule@[short caption]long long text

So, there is no error during compilation but instead I just see the line above in the text under the image. Do you have an Idea what I am doing wrong here? I think I miss here to escape some signs. Searching the web does not give my proper answer on how to use \newcommand for more complex commands like that one above, maybe it is even impossible to wrap it up like this. If yes, does anyone know an alternative?

kind regards,

Stephan

• You seem to be forgetting \makeatletter and \makeatother. – egreg Nov 26 '17 at 14:44

You need to use \makeatletter and \makeatother in different places.

\documentclass{article}
\usepackage[numbers]{kantlipsum}
\usepackage{caption}

\makeatletter
\newenvironment{figurewithlongcaption}
{\center\captionsetup{singlelinecheck=false}}
{\endcenter}

\newcommand{\longcaption}{\@dblarg\long@@caption}
\def\long@@caption[#1]#2{%
\setbox\z@=\vbox{
\let\caption@rule\relax
\captionof{figure}[#1]{#2}
\global\skip\@ne\lastskip\unskip
\global\setbox\@ne\lastbox
}
\vspace{\abovecaptionskip}
\unvbox\z@
\setbox\z@\hbox{\unhbox\@ne\unskip\unskip\unpenalty\global\setbox\@ne\lastbox}%
\unvbox\@ne
\vskip\skip\@ne
}
\makeatother

\begin{document}

\begin{figurewithlongcaption}

\rule{0.5\textwidth}{0.75\textheight}

\longcaption[Short caption]{\kant[1-4]\label{whatever}}

\end{figurewithlongcaption}

Reference: \ref{whatever}

\end{document}

• thanks for the quick answer, I'll try it out and report the result. Maybe in advance, do I really need a new environment for the graphic as well? Maybe it's part of the sollution but I was just confused that defining the \longcaption command is not enough. nevertheless, I'll try it out now... :-) – Stephan Weiser Nov 26 '17 at 16:54
• it works like a charm, thank you for the quick response. In the next days I'll try then to set up the list of figures and hopefully it will work with that ;-) thank you so far – Stephan Weiser Nov 26 '17 at 17:10