[Tex/LaTex] \captionsetup for figure* and table*

captionsfloatspositioning

For a book I am writing I set my captions up to be in the margin, which works quite nicely. But I have one problem with the distinction of figure and figure* environment in terms of \captionsetup. The first should have some negative vertical space at the top (caption format sidenote), the second should have a positive one (caption format sidenotebelow).

So how can I use \captionsetup with different configurations for figure and figure* (also table and table* of course)?

\documentclass[twoside=semi]{scrreprt}

\usepackage[a4paper,left=24.8mm,top=27.4mm,headsep=2\baselineskip,textwidth=107mm,marginparsep=8.2mm,marginparwidth=49.4mm,%
    textheight=49\baselineskip,headheight=\baselineskip]{geometry}%
\usepackage{caption,subcaption}
\usepackage{scrlayer-notecolumn}
\usepackage{xparse,etoolbox}
\usepackage{graphicx}

\RedeclareNoteColumn[position=\oddsidemargin+1in+\textwidth+\marginparsep,width=\marginparwidth,font=\footnotesize]{marginpar}
\DeclareDocumentCommand{\xmarginnote}{O{0pt} +m}{%
    \makenote[marginpar]{\hbox{}\vskip#1\setlength{\parindent}{0.5pc}\setlength{\parskip}{0pt}\noindent #2}%
}
\AtEndPreamble{%
    \DeclareDocumentEnvironment{figure*}{o}{\begin{figure}[#1]\begin{addmargin}[0pt]{-\dimexpr\marginparwidth+\marginparsep}}
        {\end{addmargin}\end{figure}}
    \DeclareDocumentEnvironment{table*}{o}{\begin{table}[#1]\begin{addmargin}[0pt]{-\dimexpr\marginparwidth+\marginparsep}}
        {\end{addmargin}\end{table}}
}%
\captionsetup{compatibility=false}%
\DeclareCaptionFormat{sidenote}{\protect\xmarginnote[-\baselineskip]{#1#2#3}}%
\DeclareCaptionFormat{sidenotebelow}{\protect\xmarginnote[1.5\baselineskip]{#1#2#3}}%
\DeclareCaptionStyle{sidecap}{parskip=0pt,skip=0pt,position=below,labelfont={footnotesize,bf},font=footnotesize,%
    singlelinecheck=off}%
\DeclareCaptionStyle{subsidecap}{format=plain,parskip=0pt,skip=0pt,position=below,labelfont={scriptsize,bf},labelformat=parens,labelsep=space,%
    font=scriptsize,justification=centering,singlelinecheck=off}%
\captionsetup[figure]{style=sidecap,format=sidenote}\captionsetup[table]{style=sidecap,format=sidenote}%
% here it doesn't work
\captionsetup[figure*]{style=sidecap,format=sidenotebelow}\captionsetup[table*]{style=sidecap,format=sidenotebelow}%
\captionsetup[sub]{style=subsidecap}%

\begin{document}
    \begin{figure}[h]
        \centering
        \includegraphics[width=\textwidth]{example-image-A.pdf}
        \caption{Working caption}
    \end{figure}
    \begin{figure*}[h]
        \centering
        \includegraphics[width=\dimexpr\textwidth+\marginparwidth]{example-image-A.pdf}
        \caption{Caption to high}
    \end{figure*}
\end{document}

Best Answer

As Schweinebacke pointed out in the comments, there was an easy solution I was not aware of. That is to just move the \captionsetupinto the environment definition.

Here's the corrected code:

\documentclass[twoside=semi]{scrreprt}

\usepackage[a4paper,left=24.8mm,top=27.4mm,headsep=2\baselineskip,textwidth=107mm,marginparsep=8.2mm,marginparwidth=49.4mm,%
    textheight=49\baselineskip,headheight=\baselineskip]{geometry}%
\usepackage{caption,subcaption}
\usepackage{scrlayer-notecolumn}
\usepackage{xparse,etoolbox}
\usepackage{graphicx}

\RedeclareNoteColumn[position=\oddsidemargin+1in+\textwidth+\marginparsep,width=\marginparwidth,font=\footnotesize]{marginpar}
\DeclareDocumentCommand{\xmarginnote}{O{0pt} +m}{%
    \makenote[marginpar]{\hbox{}\vskip#1\setlength{\parindent}{0.5pc}\setlength{\parskip}{0pt}\noindent #2}%
}
\AtEndPreamble{%
    \DeclareDocumentEnvironment{figure*}{o}{\begin{figure}[#1]%
            \captionsetup{style=sidecap,format=sidenotebelow}\begin{addmargin}[0pt]{-\dimexpr\marginparwidth+\marginparsep}}
        {\end{addmargin}\end{figure}}
    \DeclareDocumentEnvironment{table*}{o}{\begin{table}[#1]%
            \captionsetup{style=sidecap,format=sidenotebelow}\begin{addmargin}[0pt]{-\dimexpr\marginparwidth+\marginparsep}}
        {\end{addmargin}\end{table}}
}%
\captionsetup{compatibility=false}%
\DeclareCaptionFormat{sidenote}{\protect\xmarginnote[-\baselineskip]{#1#2#3}}%
\DeclareCaptionFormat{sidenotebelow}{\protect\xmarginnote[1.5\baselineskip]{#1#2#3}}%
\DeclareCaptionStyle{sidecap}{parskip=0pt,skip=0pt,position=below,labelfont={footnotesize,bf},font=footnotesize,%
    singlelinecheck=off}%
\DeclareCaptionStyle{subsidecap}{format=plain,parskip=0pt,skip=0pt,position=below,labelfont={scriptsize,bf},labelformat=parens,labelsep=space,%
    font=scriptsize,justification=centering,singlelinecheck=off}%
\captionsetup[figure]{style=sidecap,format=sidenote}\captionsetup[table]{style=sidecap,format=sidenote}%
\captionsetup[sub]{style=subsidecap}%

\begin{document}
    \begin{figure}[h]
        \centering
        \includegraphics[width=\textwidth]{example-image-a}
        \caption{Working caption}
    \end{figure}
    \begin{figure*}[h]
        \centering
        \includegraphics[width=\dimexpr\textwidth+\marginparwidth]{example-image-a}
        \caption{Caption to high}
    \end{figure*}
\end{document}
Related Question