9

I am using the command \sidecaption from the sidenotes package with a KOMA-Script class. I'd like to format (shape, size, color) the text of the caption and its label.

According to KOMA documentation (Chap. 3, p. 118 sq.), it should be handled this way:

\setkomafont{captionlabel}{<layout>}

However, it doesn't work. Any clue how to change it?


MWE

enter image description here

(The label Figure 1: should appear in bold and small caps)

\documentclass{scrartcl}
    \usepackage{lipsum}
    \usepackage{graphicx}
    \usepackage{sidenotes}

    \setkomafont{captionlabel}{\scshape\bfseries}

\begin{document}
    \lipsum
    \begin{figure}[htb]
        \sidecaption[][-2\baselineskip]{This is my caption}
        \includegraphics[width=\textwidth, height=20em]{example-image-a}
    \end{figure}
    \lipsum
\end{document}
11

The sidenotes package uses package caption to define a caption style sidecaption. You can redeclare this style:

\documentclass{scrartcl}
\usepackage[T1]{fontenc}% <- added
\usepackage{lipsum}
\usepackage{graphicx}
\usepackage{sidenotes}

\DeclareCaptionStyle{sidecaption}{labelfont={sc,bf},justification=raggedright}
\begin{document}
    \lipsum
    \begin{figure}[htb]
        \sidecaption[][-2\baselineskip]{This is my caption}
        \includegraphics[width=\textwidth, height=20em]{example-image-a}
    \end{figure}
    \lipsum
\end{document}

enter image description here


Or with the KOMA-Script environment captionbeside:

\documentclass{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{graphicx}

\setkomafont{captionlabel}{\scshape\bfseries}

\usepackage{etoolbox}
\BeforeBeginEnvironment{captionbeside}{%
  \setcapindent*{0pt}%
  \setcaptionalignment{l}% needs KOMA-Script Version 3.25; workaround for older versions: \addtokomafont{caption}{\raggedright}%
}

\begin{document}
\lipsum
\begin{figure}[htb]
\begin{captionbeside}{This is my caption}
    [o]% caption on the outer document side
    [\dimexpr\textwidth+\marginparwidth+\marginparsep\relax]% enlarge the used width
    [0pt]*% align with the inner margin
  \includegraphics[width=\textwidth, height=20em]{example-image-a}
\end{captionbeside}
\end{figure}
\lipsum
\end{document}

enter image description here

Or with option captions=topbeside and a raised image:

\documentclass[%
  captions=topbeside% change the position of the sidecaption to the top baseline
]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\usepackage{graphicx}

\setkomafont{captionlabel}{\scshape\bfseries}

\usepackage{etoolbox}
\BeforeBeginEnvironment{captionbeside}{%
  \setcapindent*{0pt}%
  \setcaptionalignment{l}% needs KOMA-Script Version 3.25; workaround for older versions: \addtokomafont{caption}{\raggedright}%
}

\begin{document}
\lipsum
\begin{figure}[htb]
\begin{captionbeside}{This is my caption}
    [o]% caption on the outer document side
    [\dimexpr\textwidth+\marginparwidth+\marginparsep\relax]% enlarge the used width
    [0pt]*% align with the inner margin
  \raisebox
    {\dimexpr-\totalheight+\ht\strutbox\relax}
    {\includegraphics[width=\textwidth, height=20em]{example-image-a}}
\end{captionbeside}
\end{figure}
\lipsum
\end{document}

enter image description here

  • Thank you: using KOMA-script embedded macros is the solution I will follow. I should have read couple of pages more in the documentation... Thank you aslo for the \raisebox{\dimexpr-\totalheight+\baselineskip\relax}{<picture>} trick: I thought it would work directly with \KOMAoptions{captions=besidetop} but it's obviously not the case. However, the top of the F (in "Figure") is not pixel-vertical-aligned with the top of the picture. "+baselineskip" seems a bit to much, but I did not found how to fine-tune it (with relative lengths). Do you know how to?! – ebosi Jul 6 '16 at 8:08
  • 1
    @ebo I have changed my answer. Use \raisebox{\dimexpr-\totalheight+\ht\strutbox\relax}{<picture>}. The \raisbox` is needed because the baseline of the picture is at its bottom. – esdd Jul 6 '16 at 8:18
5

KOMA-Script has its own captionbeside environment (see manual page 121). With that \addkomafont changed the font for the caption as you wish.

Please see the following MWE:

\documentclass{scrartcl}

\usepackage{lipsum}
\usepackage{graphicx}
%\usepackage{sidenotes}

\setkomafont{captionlabel}{\scshape\bfseries}


\begin{document}
\lipsum
\begin{figure}[htb]
\begin{captionbeside}{This is my caption}%
   [r]
  \includegraphics[width=.7\textwidth]{example-image-a}
\end{captionbeside}
\end{figure}

\lipsum
\end{document}

with the result:

enter image description here

If you want to use \sidecaption you have to use package caption to influence the formating of the caption because package sidenotes calls caption internally ...

0

Another solution is the mcaption package.

\documentclass{scrartcl}
\usepackage{blindtext}
\usepackage{graphicx}
\usepackage[top]{mcaption}
\usepackage{caption}

\addtokomafont{captionlabel}{\bfseries\scshape}
\addtokomafont{caption}{\itshape}
\setcapindent*{0em}

\begin{document}
\blindtext
\begin{figure}[htb]
\begin{margincap}
    \caption{This is my caption}
    \includegraphics[width=\textwidth, height=20em]{example-image-a}
\end{margincap}
\end{figure}
\blindtext
\end{document}

enter image description here

  • The caption in the output overflows the page width it's outside the page even with an image width of 0.7\textwidth – SAM Jul 5 '16 at 20:12
  • the OP wanted only "figure 1:" to be affected not the whole caption you want to remove \setkomafont{caption}{\bfseries}. – SAM Jul 5 '16 at 20:22
  • @SAM: "I'd like to format (shape, size, color) the text of the caption and its label.". The caption goes over the border of the page because the margin is nod wide enough ... the width of the image is irrelevant here ... – Stefan Braun Jul 5 '16 at 22:48
  • 1
    @SAM & Stefan: indeed, my wording was a bit ambiguous. I had in mind ""I'd like to format (shape, size, color) the text of the caption and -- independently -- of its label." Thank you for pointing to a package that explicitly works with KOMA-Script classes. Even if I'll give a try to KOMA-embedded solution (see other answers) in order to have a better control on what happens, this solution appears easier to use out of the box and I'll keep it in mind! – ebosi Jul 6 '16 at 8:17
0

To complete esdd's great answer, I define below two environments margincaptionbottom and margincaptiontop that wrap up all settings together.

These environments create a inner ragged caption in the outer margin of the document. The caption is flushed bottom or top, respectively.
Note that the environment work for both one-sided and two-sided documents.

enter image description here


Syntax

\begin{margincaptionbottom}[Optional different caption for the LoT]{Caption to be displayed}
    <your picture here>
\end{margincaptionbottom}

(idem for margincaptiontop)


Environment definition to put in preamble

\usepackage{etoolbox}
\usepackage{environ}

\makeatletter
\if@twoside%
   \newcommand{\raggedinner}{\ifthispageodd{\raggedright}{\raggedleft}}
\else%
   \newcommand{\raggedinner}{\raggedright}
\fi%
\makeatother

\BeforeBeginEnvironment{captionbeside}{\setcapindent*{0pt}\addtokomafont{caption}{\raggedinner}}
\KOMAoptions{captions=outerbeside}

\NewEnviron{margincaptionbottom}[2][]{%
    \KOMAoptions{captions=bottombeside}
    \begin{captionbeside}%
        [#1]
        {#2}%
        [o]%
        [\dimexpr\textwidth+\marginparwidth+\marginparsep\relax]%
        [0pt]*%
        \BODY
    \end{captionbeside}%
}

\NewEnviron{margincaptiontop}[2][]{%
    \KOMAoptions{captions=topbeside}
    \begin{captionbeside}%
        [#1]
        {#2}%
        [o]%
        [\dimexpr\textwidth+\marginparwidth+\marginparsep\relax]%
        [0pt]*%
        \raisebox{\dimexpr\ht\strutbox-\totalheight\relax}{\BODY}%
    \end{captionbeside}%
}

MWE

\documentclass[two side]{scrartcl}
    %standard packages
    \usepackage{graphicx}
    \usepackage{lipsum}

    %Own definition of caption style
    \setkomafont{captionlabel}{\scshape\sffamily\small}
    \setkomafont{caption}{\normalfont\small}
    \renewcommand*{\captionformat}{\quad}

%%%% To be copied to define environments 'margincaptionbottom' and 'margincaptiontop'
    \usepackage{etoolbox}
    \usepackage{environ}

    \makeatletter
    \if@twoside%
       \newcommand{\raggedinner}{\ifthispageodd{\raggedright}{\raggedleft}}
    \else%
       \newcommand{\raggedinner}{\raggedright}
    \fi%
    \makeatother

    \BeforeBeginEnvironment{captionbeside}{\setcapindent*{0pt}\addtokomafont{caption}{\raggedinner}}
    \KOMAoptions{captions=outerbeside}

    \NewEnviron{margincaptionbottom}[2][]{%
        \KOMAoptions{captions=bottombeside}
        \begin{captionbeside}%
            [#1]
            {#2}%
            [o]%
            [\dimexpr\textwidth+\marginparwidth+\marginparsep\relax]%
            [0pt]*%
            \BODY
        \end{captionbeside}%
    }

    \NewEnviron{margincaptiontop}[2][]{%
        \KOMAoptions{captions=topbeside}
        \begin{captionbeside}%
            [#1]
            {#2}%
            [o]%
            [\dimexpr\textwidth+\marginparwidth+\marginparsep\relax]%
            [0pt]*%
            \raisebox{\dimexpr\ht\strutbox-\totalheight\relax}{\BODY}%
        \end{captionbeside}%
    }
%%%% end of definition
\title{MWE for \texttt{margincaptionbottom} and \texttt{margincaptiontop} environments}
\author{ebo}    
\begin{document}
    \maketitle
    \clearpage

    \section*{Bottom beside left}
    \lipsum[1]
    \begin{figure}[!h]
        \begin{margincaptionbottom}[Bottom left]{This is a bottom caption in the left margin}
        \includegraphics[width=\textwidth, height=210pt]{example-image-a}
        \end{margincaptionbottom}
    \end{figure}
    \lipsum[2]
    \clearpage

    \section*{Bottom beside right}
    \lipsum[1]
    \begin{figure}[!h]
        \begin{margincaptionbottom}[Bottom right]{This is a bottom caption in the right margin}
        \includegraphics[width=\textwidth, height=210pt]{example-image-a}
        \end{margincaptionbottom}
    \end{figure}
    \lipsum[2]
    \clearpage

    \section*{Top beside left}
        \lipsum[1]
        \begin{figure}[!h]
            \begin{margincaptiontop}[Top left]{This is a top caption in the left margin}
            \includegraphics[width=\textwidth, height=210pt]{example-image-a}
            \end{margincaptiontop}
        \end{figure}
        \lipsum[2]
        \clearpage  

    \section*{Top beside right}
        \lipsum[1]
        \begin{figure}[!h]
            \begin{margincaptiontop}[Top right]{This is a top caption in the right margin}
            \includegraphics[width=\textwidth, height=210pt]{example-image-a}
            \end{margincaptiontop}
        \end{figure}
        \lipsum[2]
        \clearpage

    \listoffigures
\end{document}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.