Wie platziere ich Text am Ende einer Beschriftung?

Wie platziere ich Text am Ende einer Beschriftung?

Ich würde den Text mit dem Copyright-Hinweis zu einem Bild gerne aktuell in das Abbildungsverzeichnis setzen, aber vielleicht muss ich ihn ändern und (auch) unter jedes Bild setzen.

Dieses MWE funktioniert einwandfrei:

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\newcommand\after[1]{#1}
\newcommand\afterlof{; \after}
\newcommand\aftercap{ }%for optional use
\begin{document}
\begin{figure}[htb]
    \includegraphics[width=.5\textwidth]{example-image-a}
\def\after{the copyright for this image}
    \caption%
     [caption for LOF\afterlof]%
     {caption below the image\aftercap}
\end{figure}

\begin{figure}[htb]
    \includegraphics[width=.5\textwidth]{example-image-a}
\def\after{the copyright for another image}
    \caption%
     [caption for LOF\afterlof]%
     {caption below the image\aftercap}
\end{figure}
\listoffigures
\end{document}

MWE

Meine Frage:

Gibt es einen Wegnichtafterlofdie beiden Befehle ( und ) manuell in jede Überschrift einzufügen aftercap? So dass die Überschrift per Definition immer so aussieht \caption[ \afterlof]{ \aftercap}?

Antwort1

Ich schlage eine Schlüssel-Wert-Schnittstelle vor:

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\xcaption}{O{}m}
 {% #1 are options, #2 is the main caption text
  \tl_set:Nn \l_lukascb_caption_text_tl { #2 }
  \keys_set:nn { lukascb/caption }
   {
    loftext = #2,
    #1
   }
  \lukascb_make_caption:VVVV
    \l_lukascb_caption_text_tl
    \l_lukascb_caption_aftercap_tl
    \l_lukascb_caption_loftext_tl
    \l_lukascb_caption_afterlof_tl
 }

\tl_new:N \l_lukascb_caption_text_tl

\keys_define:nn { lukascb/caption }
 {
  loftext  .tl_set:N = \l_lukascb_caption_loftext_tl,
  afterlof .tl_set:N = \l_lukascb_caption_afterlof_tl,
  aftercap .tl_set:N = \l_lukascb_caption_aftercap_tl,
 }

\cs_new_protected:Nn \lukascb_make_caption:nnnn
 {
  \caption
    [ #3 \tl_if_empty:nF { #4 } { ;~#4 } ]
    { #1 \tl_if_empty:nF { #2 } { ;~#2 } }
 }
\cs_generate_variant:Nn \lukascb_make_caption:nnnn { VVVV }
\ExplSyntaxOff

\begin{document}

\listoffigures

\section{Main text}

\begin{figure}[!htp]
\centering

\includegraphics[width=.2\textwidth]{example-image-a}

\xcaption[
  loftext=caption for LOF,
  afterlof=copyright for this image,
]{caption below the image}

\end{figure}

\begin{figure}[!htp]
\centering

\includegraphics[width=.2\textwidth]{example-image-a}

\xcaption[
  afterlof=the copyright,
  aftercap=something else,
]{caption below the image}

\end{figure}

\end{document}

Wenn Sie nicht angeben loftext, wird der Text der Hauptunterschrift verwendet. Der Wert von afterlofwird im Abbildungsverzeichnis angehängt, der Wert von aftercapan die Hauptunterschrift.

Somit müssen Sie die Schlüssel nur noch hinzufügen, wenn Sie diese verwenden möchten.

Bildbeschreibung hier eingeben

Eine Erweiterung für \subcaptionbox, die Syntax ist ähnlich: Das erste optionale Argument \subcaptionboxwird zu einer Liste von Optionen.

\documentclass{article}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{subcaption}
\usepackage{xparse}

\captionsetup[sub]{list=true}

\ExplSyntaxOn
\NewDocumentCommand{\xcaption}{O{}m}
 {% #1 are options, #2 is the main caption text
  \tl_set:Nn \l_lukascb_caption_text_tl { #2 }
  \lukascb_make_caption:Nnn \__lukascb_make_caption:VVVV { #1 } { #2 }
 }

\NewDocumentCommand{\xsubcaptionbox}{O{}m}
 {% #1 are options, #2 is the main caption text
  \tl_set:Nn \l_lukascb_caption_text_tl { #2 }
  \lukascb_make_caption:Nnn \__lukascb_make_subcaption:VVVV { #1 } { #2 }
 }

\cs_new_protected:Nn \lukascb_make_caption:Nnn
 {
  \keys_set:nn { lukascb/caption }
   {% first clear the variables
    loftext = #3,
    aftercap = {},
    afterlof = {},
    % then load the given ones
    #2
   }
  #1 \l_lukascb_caption_text_tl \l_lukascb_caption_aftercap_tl
     \l_lukascb_caption_loftext_tl \l_lukascb_caption_afterlof_tl
 }

\tl_new:N \l_lukascb_caption_text_tl

\keys_define:nn { lukascb/caption }
 {
  loftext  .tl_set:N = \l_lukascb_caption_loftext_tl,
  afterlof .tl_set:N = \l_lukascb_caption_afterlof_tl,
  aftercap .tl_set:N = \l_lukascb_caption_aftercap_tl,
 }

\cs_new_protected:Nn \__lukascb_make_caption:nnnn
 {
  \caption
    [ #3 \tl_if_empty:nF { #4 } { ;~#4 } ]
    { #1 \tl_if_empty:nF { #2 } { ;~#2 } }
 }
\cs_generate_variant:Nn \__lukascb_make_caption:nnnn { VVVV }
\cs_new_protected:Nn \__lukascb_make_subcaption:nnnn
 {
  \subcaptionbox
    [ #3 \tl_if_empty:nF { #4 } { ;~#4 } ]
    { #1 \tl_if_empty:nF { #2 } { ;~#2 } }
 }
\cs_generate_variant:Nn \__lukascb_make_subcaption:nnnn { VVVV }
\ExplSyntaxOff

\begin{document}

\listoffigures

\section{Main text}

\begin{figure}[!htp]
\centering

\includegraphics[width=.2\textwidth]{example-image-a}

\xcaption[
  loftext=caption for LOF,
  afterlof=copyright for this image,
]{caption below the image}

\end{figure}

\begin{figure}[!htp]
\centering

\xsubcaptionbox[
  afterlof=the copyright A,
  aftercap=something else,
]{subcaption}{\includegraphics[width=.2\textwidth]{example-image-a}}\qquad
\xsubcaptionbox[
  afterlof=the copyright B,
]{subcaption}{\includegraphics[width=.2\textwidth]{example-image-b}}

\xcaption[
  afterlof=the copyright,
]{caption below the image}

\end{figure}

\end{document}

Bildbeschreibung hier eingeben

Antwort2

Ein schneller „Hack“ durch eine Neudefinition von \captionmit und Testen, ob xparseusw. definiert ist.\NewDocumentCommand\after

Verwenden Sie \def\after{...}oder \newcommand{\after}innerhalb der Abbildungsumgebung, sodass der Befehl nicht außerhalb definiert ist und der Test den falseZweig ausführt, ohne zusätzlichen Text zu drucken.

\documentclass{article}
\usepackage{graphicx}
\usepackage{xparse}

\usepackage{caption}

\makeatletter
\let\caption@@caption\caption
\RenewDocumentCommand{\caption}{so+m}{%
  \IfBooleanTF{#1}{%
    \caption@@caption*{#3}%
  }{%
    \IfValueTF{#2}{%
      \caption@@caption[#2\@ifundefined{after}{}{\afterlof}]{#3~\@ifundefined{after}{}{\aftercap}}%
    }{%
      \caption@@caption{#3~\csname aftercap\endcsname}
    }%
  }%
}

\makeatother

\newcommand\afterlof{; \after}
\newcommand\aftercap{ }%for optional use
\begin{document}
\begin{figure}[htb]
    \includegraphics[width=.5\textwidth]{example-image-a}
    \def\after{the copyright for this image}
    \caption[caption for LOF]{caption below the image}
\end{figure}

\begin{figure}[htb]
    \includegraphics[width=.5\textwidth]{example-image-a}
    \def\after{Another copyright for another image}
    \def\aftercap{Some info}
    \caption[caption for LOF]{Normal caption below the image}

    \caption{Another caption below the image}
\end{figure}
\listoffigures
\end{document}

Bildbeschreibung hier eingeben

Eine alternative Version mit Schlüssel-Wert-Syntax, bitte beachten Sie das 2. optionale Argument des \captionArguments!

\documentclass{article}
\usepackage{graphicx}
\usepackage{xparse}

\usepackage{caption}

\ExplSyntaxOn
\prop_new:N \l_lukascb_caption_prop 

\keys_define:nn {lukascb} {%
  afterlof .code:n={\prop_put:Nnn \l_lukascb_caption_prop {afterlof} {#1}},
  aftercap .code:n={\prop_put:Nnn \l_lukascb_caption_prop {aftercap} {#1}},
  afterlofsep .code:n={\prop_put:Nnn \l_lukascb_caption_prop {afterlofsep} {#1}}
}

\NewDocumentCommand{\SetLocalCaptionOptions}{m}{%
  \prop_clear:N \l_lukascb_caption_prop 
  \keys_set:nn {lukascb} {afterlofsep={;},#1}
}

\cs_new:Npn \retrievecaptiondata #1{%
  \prop_item:Nn \l_lukascb_caption_prop {#1}%
}


% Normally a `\prg_new_conditional, but for quickness much shorter..

\cs_new:Npn \ifcaptiondatagiven #1#2#3{%
  \prop_if_in:NnTF \l_lukascb_caption_prop {#1} {%
    #2%
  }{#3}
}
\ExplSyntaxOff



\makeatletter
\let\caption@@caption\caption
\RenewDocumentCommand{\caption}{so+mO{}}{%
  \SetLocalCaptionOptions{#4}
  \IfBooleanTF{#1}{%
    \caption@@caption*{#3}%
  }{%
    \IfValueTF{#2}{%
      \caption@@caption[#2\ifcaptiondatagiven{afterlof}{\retrievecaptiondata{afterlofsep}\retrievecaptiondata{afterlof}}{}]{#3~\retrievecaptiondata{aftercap}}%
    }{%
      \caption@@caption{#3~\retrievecaptiondata{aftercap}}
    }%
  }%
}

\makeatother

\begin{document}
\begin{figure}[htb]
    \includegraphics[width=.5\textwidth]{example-image-a}
    \caption[First caption for LOF]{caption below the image}[afterlof={\textcopyright\ By me!}, aftercap={And now for something completely different}]
\end{figure}

\begin{figure}[htb]
    \includegraphics[width=.5\textwidth]{example-image-a}
    \caption[caption for LOF]{Normal caption below the image}[afterlofsep={\S}]

    \caption{Another caption below the image}[aftercap={Nudge nudge}]
\end{figure}
\listoffigures
\end{document}

Bildbeschreibung hier eingeben

verwandte Informationen