2

I would like to create a caption style for my figures. Currently, I try to do it using the caption package. The caption should be typeset in the margin paragraph. The caption should look like this: side caption with a lettrine (Grey rectangle on two lines containing "Fig. x" in bold with the text wrapping around).

Until now I tried to create it with the lettrine package which outside a caption does produce good result (the image was done that way).

This code does work (but it is not a true caption...):

\usepackage{lettrine}
\usepackage{tikz}
...
\marginpar{
    \lettrine
        [lhang=0, lines=2, findent=.5em, nindent=0em ]
        {\normalsize\tikz{
            \node[anchor=west, inner sep=2mm, fill=Grey, font=\bfseries, text=red]
                {Fig. 1};
        }}{}%
    text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
}

However, it does not work when used as a caption style! Why?

\usepackage{caption}
\usepackage{xparse}
\usepackage{lettrine}
\usepackage{tikz}
\DeclareCaptionLabelFormat{myLabelStyle}{%
    \lettrine
        [lhang=0, lines=2, findent=.5em, nindent=0em ]
        {\normalsize\tikz{
            \node[anchor=west, inner sep=2mm, fill=Grey, font=\bfseries, text=red]
                {#1 #2}; % #1 is the float name (here "Fig.") and #2 is the float number
    }}{}%
}

\DeclareCaptionStyle{myReportCaptionStyle}{labelsep=none, labelformat=myLabelStyle}
\captionsetup*[figure]{style=myReportCaptionStyle}
\DeclareDocumentCommand{\sideCaptionOf}{m m}{\marginpar{\captionof{#1}{#2}}}

MWE:

\documentclass{article}
\usepackage[
    a4paper,
    twoside,
    top=20mm,
    bottom=20mm,
    inner=20mm,
    outer=20mm,
    includemp,
    marginparwidth=52mm,
    marginparsep=8mm,
    headsep=10mm,
    footskip=8mm,
    headheight=12.2pt,
]{geometry}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\usepackage{lettrine}
\usepackage{caption}
\usepackage{tikz}
\usepackage{xparse}

\DeclareCaptionLabelFormat{myLabelStyle}{%
    \lettrine
        [lhang=0, lines=2, findent=.5em, nindent=0em ]
        {\normalsize\tikz{
            \node[anchor=west, inner sep=2mm, fill=Grey, font=\bfseries, text=red]
                {#1 #2};
    }}{}%
}

\DeclareCaptionStyle{myReportCaptionStyle}{labelsep=none, labelformat=myLabelStyle}
\captionsetup*[figure]{style=myReportCaptionStyle}
\DeclareDocumentCommand{\sideCaptionOf}{m m}{\marginpar{\captionof{#1}{#2}}}


\begin{document}
% Working
\newpage
\marginpar{
    \lettrine[lhang=0, lines=2, findent=.5em, nindent=0em ]{\normalsize\tikz{\node[anchor=west, inner sep=2mm, fill=Grey, font=\bfseries, text=red] {Fig. 1};}}{}%
    text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text 
}
\begin{figure}[ht!]
    \rule{\linewidth}{6cm}
\end{figure}

% Not working
\newpage
\begin{figure}
    \rule{\linewidth}{6cm}
    \sideCaptionOf{figure}{My caption text}
\end{figure}
\end{document}

Final Note: As I understand it, the problem comes from the fact that one cannot use a parshape in a caption. But why? And how to produce the same effect then?

  • Where ist \marginElement being defined? – Keks Dose Mar 17 '16 at 17:46
  • Sorry about that, marginElement behaved as a marginpar command but added a footnote mark and changed the alignment depending on the page being odd or even. I replaced it with marginpar commands for clarity and simplicity. – HarveyShepp Mar 19 '16 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.