5

do you know how to draw a vertical line before the caption text but after the figure label like in the example below ? enter image description here

The vertical rule height must be the same as the height of the caption text box.

I'am using scrbook with the caption package.

Many thanks

1

1 Answer 1

5

The following example defines a new caption format HangRedLine. Starting point of the format was the definition for the caption format hang. It puts the full caption in a box and overlays it with the red rule, which extends to the height and depth of the box.

\documentclass{scrbook}
\usepackage{tgpagella}% TeX Gyre Palladio (TeX Gyre's Palatino version)
\usepackage{caption}
\usepackage{xcolor}

\definecolor{CaptionBlue}{HTML}{005090}
\newcommand*{\RedCaptionLine}{%
  \leavevmode
  \llap{\color{red}\vline width 1pt\relax\,}%
}

\makeatletter

\newcommand*{\CaptionVLineWidth}{1pt}
\newcommand*{\CaptionVLineSep}{.2em}

\newcommand*{\CaptionVLine}{%
  \noindent
  \kern\dimexpr-\CaptionVLineSep-\CaptionVLineWidth\relax
  \textcolor{red}{\vline width\CaptionVLineWidth}%
  \kern\CaptionVLineSep\relax
}

\DeclareCaptionFormat{HangRedLine}[%
  #1#2\CaptionVLine#3\par % Single line captions
]{% Multi-line captions
  \caption@ifin@list\caption@lsepcrlist\caption@lsepname{%
    \caption@Error{%
      The option `labelsep=\caption@lsepname' does not work\MessageBreak
      with `format=hang'}
  }{%
    \sbox0{%
      \parbox[t]{\linewidth}{%
      \@hangfrom{#1#2}%
      \advance\caption@parindent\hangindent
      \advance\caption@hangindent\hangindent
      \xdef\CaptionHangIndent{\the\hangindent}%
      \caption@@par#3\par
      }%
    }%
    \noindent
    \kern\CaptionHangIndent\relax
    \CaptionVLine
    \kern-\CaptionHangIndent\relax
    \usebox0%
  }%
}
\makeatother

\captionsetup{
  labelsep=quad,
  labelfont={color=CaptionBlue},
  format=HangRedLine,
}

\begin{document}
\stepcounter{chapter}
\stepcounter{chapter}
\stepcounter{figure}

\begin{figure}
\caption{Illustration of the induced angle of attack
  and the induced drag caused by the downwash velocity $w$}
\caption{Short caption}
\end{figure}
\end{document}

Result


With subfigures:

\documentclass{scrbook}
\usepackage{tgpagella}% TeX Gyre Palladio (TeX Gyre's Palatino version)
\usepackage{caption}
\usepackage{subcaption}
\usepackage{xcolor}

\definecolor{CaptionBlue}{HTML}{005090}
\newcommand*{\RedCaptionLine}{%
  \leavevmode
  \llap{\color{red}\vline width 1pt\relax\,}%
}

\makeatletter

\newcommand*{\CaptionVLineWidth}{1pt}
\newcommand*{\CaptionVLineSep}{.2em}

\newcommand*{\CaptionVLine}{%
  \noindent
  \kern\dimexpr-\CaptionVLineSep-\CaptionVLineWidth\relax
  \textcolor{red}{\vline width\CaptionVLineWidth}%
  \kern\CaptionVLineSep\relax
}

\DeclareCaptionFormat{HangRedLine}[%
  % Single line captions
  #1%
  \sbox0{#3}%
  \ifdim\wd0>0pt %
    #2\CaptionVLine#3%
  \fi
  \par
]{% Multi-line captions
  \caption@ifin@list\caption@lsepcrlist\caption@lsepname{%
    \caption@Error{%
      The option `labelsep=\caption@lsepname' does not work\MessageBreak
      with `format=hang'}
  }{%
    \sbox0{%
      \parbox[t]{\linewidth}{%
      \@hangfrom{#1#2}%
      \advance\caption@parindent\hangindent
      \advance\caption@hangindent\hangindent
      \xdef\CaptionHangIndent{\the\hangindent}%
      \caption@@par#3\par
      }%
    }%
    \noindent
    \kern\CaptionHangIndent\relax
    \CaptionVLine
    \kern-\CaptionHangIndent\relax
    \usebox0%
  }%
}
\makeatother


\captionsetup{
  labelsep=quad,
  labelfont={color=CaptionBlue},
  format=HangRedLine,
}
\captionsetup[sub]{
  labelsep=quad,
}

\begin{document}
\stepcounter{chapter}
\stepcounter{chapter}
\stepcounter{figure}

\begin{figure}
\begin{minipage}[b]{.5\linewidth}
  \centering
  \subcaption{A subfigure}
\end{minipage}%
\begin{minipage}[b]{.5\linewidth}
  \centering
  \subcaption{}
\end{minipage}
\caption{Illustration of the induced angle of attack
  and the induced drag caused by the downwash velocity $w$}
\caption{Short caption}
\end{figure}
\end{document}

Result with subfigures

3
  • Thank you for your answer. I like the redefinition of the hang caption format rather than using tikz and tabular like it was suggested before.
    – BxlAdil
    Oct 22, 2016 at 23:24
  • Is there a way to remove the red lines from the sub-captions (a), (b), etc.... while preserving the functions of the caption package to keep the sub-labels blue ?
    – BxlAdil
    Oct 23, 2016 at 0:13
  • @BxlAdil Just add format=default (or whatever caption format is wanted for sub-captions) inside the \captionsetup[sub]{...} part.
    – user2574
    Oct 23, 2016 at 16:15

You must log in to answer this question.

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