1

Per my question here, I have added a \caption inside a \wrapfigure representing a sidebar so that I can later refer to it using \nameref{name}. However, I now see the caption above the sidebar title:

enter image description here

Is there a way I can declare a caption but not actually have it display anything, for no reason other than being able to refer users to it via a \nameref?

I thought of changing my environment so that it uses \caption*{} to output the sidebar's title, but then the \ref{} no longer picks it up correctly (it instead picks up the current chapter).

Here is a complete example:

\documentclass{book}

\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{hyperref}
\usepackage{color}

\newcommand{\dummy}{}

\newenvironment*{lsidebar}[2][0.5]
{
    \renewcommand{\dummy}{#1}

    \wrapfigure{l}{#1\textwidth}
        \caption{#2}
        \rule{#1\textwidth}{1pt}

        \rule{#1\textwidth}{18pt}

        \vspace{-18pt}
        \centerline{\textcolor{white}{#2}}

        \vspace{5pt}
        \footnotesize
        \leftskip=5pt
        \rightskip=5pt
        \setlength{\parskip}{0.2cm}
        \setlength{\parindent}{0pt}

}
{
        \leftskip=0pt
        \rightskip=0pt
        \setlength{\parindent}{0pt}
        \rule{\dummy\textwidth}{1pt}
        \rule[.19in]{\dummy\textwidth}{2.5pt}
    \endwrapfigure
}

\begin{document}

\chapter{First}
\label{ch:First}

\begin{lsidebar}{Example Sidebar}
    \label{sb:Example}

    An example sidebar.
\end{lsidebar}

Refer to sidebar \nameref*{sb:Example}.

\end{document}

This is what gets displayed:

enter image description here

Note the redundant "Figure 1.1: Example Sidebar" above the sidebar.

Kent Boogaart
  • 664
  • 4
  • 15

1 Answers1

4

You can update both \@currentlabel and \@currentlabelname which holds the counter and name reference. Then insert a \phantomsection to mark a hyper target. This way \ref or \nameref will jump to the correct location.

enter image description here

\documentclass{book}

\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{hyperref}
\usepackage{color}

\newcommand{\dummy}{}

\makeatletter
\newenvironment*{lsidebar}[2][0.5]{%
  \renewcommand{\dummy}{#1}%
  \wrapfigure{l}{#1\textwidth}
    \renewcommand{\@currentlabel}{#2}% Update \ref marker
    \renewcommand{\@currentlabelname}{#2}% Update \nameref marker
    \phantomsection% Set hyper target
    \rule{#1\textwidth}{1pt}

    \rule{#1\textwidth}{18pt}

    \vspace{-18pt}
    \centerline{\textcolor{white}{#2}}

    \vspace{5pt}
    \footnotesize
    \leftskip=5pt
    \rightskip=5pt
    \setlength{\parskip}{0.2cm}
    \setlength{\parindent}{0pt}
  }
  {
    \leftskip=0pt
    \rightskip=0pt
    \setlength{\parindent}{0pt}
    \rule{\dummy\textwidth}{1pt}
    \rule[.19in]{\dummy\textwidth}{2.5pt}
  \endwrapfigure
}
\makeatother

\begin{document}

\chapter{First}

\begin{lsidebar}{Example Sidebar}
  \label{sb:Example}

  An example sidebar.
\end{lsidebar}

Refer to sidebar \nameref{sb:Example}.

\end{document}
Werner
  • 603,163