1

I'm trying to use the hypcap option of the caption packet or the hypcap packet by itself. If I use \caption{...} inside a float everything works fine and the hyperlink in my list of figures jumps to the top instead to the caption of the figure. But I'm using a self defined command like \mycaption{...} to enhance the list of figures with more details. With this command the hypcap package is not able to find the \caption{...} command following the \capstart, because there is only my self defined command. How can I tell hypcap or captionto search for my command instead or what do I need to add to my definition of \mycaption{...} to make it possible for hypcap to detect it ?


MWE:

\documentclass{scrreprt}

\makeatletter
\newcommand{\myrawcaption}[2]{%
    \def\n{\hfill\break}
    \refstepcounter{figure}%
    \addcontentsline{lof}{figure}%
      {\protect\numberline{\thefigure}{\ignorespaces #1}}%
    \addtocontents{lof}{\protect\nopagebreak[4]}
    \addtocontents{lof}{\begingroup\leftskip3.8em #2\par\endgroup}
    \begingroup
      \def\n{\break}
      \@makecaption{\csname fnum@figure\endcsname}{\ignorespaces #1}\par
    \endgroup

}
\makeatother

\newcommand{\myfirstcaption}[1]{\myrawcaption{#1}
{Source:~My lovely source\protect\\*
License:~The fancy license}}

\usepackage{lipsum}
\setlipsumdefault{3}
\usepackage{hyperref}
\usepackage[figure]{hypcap}
\usepackage{scrhack}

\begin{document}

\chapter{chapter}

\lipsum

\begin{figure}[htbp]
\vspace{5cm}
\myfirstcaption{this is a caption}\label{label1}
\end{figure}

\lipsum

\listoffigures

\end{document}

Error

! Package hypcap Error: You have forgotten to use \caption.

1
  • It was just for the MWE. I edited it to the full definition.
    – switches
    Aug 4, 2017 at 10:32

1 Answer 1

1

You can use \caption inside your definition of \myrawcaption:

\documentclass{scrreprt}

\makeatletter
\newcommand{\myrawcaption}[2]{%
  \caption[#1]{#1}%
  \addtocontents{\@nameuse{ext@\@captype}}{\protect\nopagebreak}%
  \addtocontents{\@nameuse{ext@\@captype}}{\protect\begingroup
    \protect\leftskip 3.8em
    #2\protect\par
    \protect\endgroup
  }%
}%
\makeatother

\newcommand{\myfirstcaption}[1]{\myrawcaption{#1}
{Source:~My lovely source\protect\\*
License:~The fancy license}}

\usepackage{lipsum}
\setlipsumdefault{3}
\usepackage{hyperref}
\usepackage[figure]{hypcap}
\usepackage{scrhack}

\begin{document}
\listoffigures
\chapter{chapter}

\lipsum

\begin{figure}[htbp]
\vspace{5cm}
\myfirstcaption{this is a caption}\label{label1}
\end{figure}

\lipsum

\listoffigures

\end{document}

If you need your changing definition of \n, I would do it using

\BeforeStartingTOC[lof]{\renewcommand*{\n\break}}

and use \protect\n inside the caption text, to add unexpanded \n into the lof file.

Instead of using \addtocontents, I would also recommend to define a new entry type figureinfo and use \addcontentsline resp. tocbasic's \addxcontentsline with this entry type. You could use \DeclareTOCStyleEntry for the definition of the new entry type.

\documentclass{scrreprt}

\makeatletter
\newcommand{\myrawcaption}[2]{%
  \caption[#1]{#1}%
  \addxcontentsline{\@nameuse{ext@\@captype}}{figureinfo}{#2}%
}%
\makeatother

\DeclareTOCStyleEntry{tocline}{figure}
\DeclareTOCStyleEntry[%
  level=1,% same a (default of) figure
  numwidth=0pt,% no number no width
  indent=3.8em,% numwidth + indent of figure
  onstartsamelevel=\nopagebreak,%
  pagenumberbox=\csname @gobble\endcsname,%
  linefill=\hfill
]{tocline}{figureinfo}

\newcommand{\myfirstcaption}[1]{\myrawcaption{#1}
{Source:~My lovely source\protect\\*
License:~The fancy license}}

\usepackage{lipsum}
\setlipsumdefault{3}
\usepackage{hyperref}
\usepackage[figure]{hypcap}
\usepackage{scrhack}

\begin{document}
\listoffigures
\chapter{chapter}

\lipsum

\begin{figure}[htbp]
\vspace{5cm}
\makeatletter
\@whilenum\value{figure}<20\do{% only for demonstrating page break in LoF
  \myfirstcaption{this is a caption}\label{label\thefigure}
}
\makeatother

\end{figure}

\lipsum

\listoffigures

\end{document}

Note: This solution does also work with the standard classes (and several other classes), but in this case you need to load package tocbasic before \DeclareTOCStyleEntry.

4
  • As I can see your solution works fine except the supressed pagebreak within a single entry (onstartsamelevel=\nobreakpage). A pagebreak should only be allowed between two different entries.
    – switches
    Aug 4, 2017 at 11:51
  • @switches Fixed. Aug 4, 2017 at 12:42
  • ! Undefined control sequence. \scr@tso@figureinfo@LastTOCLevelWasSame ...akpage
    – switches
    Aug 8, 2017 at 5:27
  • @switches My mistake. onstartsamelevel=\nopagebreak is correct and does work here. Aug 8, 2017 at 7:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .