[Tex/LaTex] captionsetup redefines the margin captions nicely, but I want other captions to be default style

captionshorizontal alignmentmarginpar

I'm writing a document with small and supporting figures placed in wide margins and larger figures in the text. I use the captions package and redefine the captions with

\captionsetup{
justification=raggedright
font=small}

and use this for my margin figure captions, which works fine. Since there are so few words on each line of the margin captions it looks ugly when Latex stretches the caption to cover the entire margin width, hence the raggedright.

Now, the problem is that the \captionsetup also redefines my "normal" captions, so that they also become raggedright, which I don't want.

So, the elements of my file are:

\usepackage{captions}

\addtolength{\evensidemargin}{1.5cm} 
\addtolength{\textwidth}{-1.5cm}
\addtolength{\marginparwidth}{20pt}
\addtolength{\marginparsep}{10pt}

\captionsetup{
justification=raggedright
font=small}

\begin{document}

text text text ...

\begin{figure}
\begin{center}    
\includegraphics{figure1}
\caption{figure 1 text, which I don't want to have a raggedright justification, since it looks quite good without it due to the many words that I can fit into one line in this type of caption.}    
\end{center}
\end{figure}

some 

more 

text 

about 

something

\marginpar{\includegraphics[width=\marginparwidth]{figure2}
\captionof{figure}{Here for figure 2, the caption will look odd when few words are stretched across the marginparwidth}}

\end{document}

So, I want the \captionof to produce a different kind of caption than what caption does when used inside a figure environment. Thankful for any hints. I had a look at the tufte class but is seems to me that all captions have a raggedright justifcation in that class.

Best Answer

You could use \captionsetup[<name>]{...} ... \captionsetup{options=<name>} to define and execute options for custom commands or environments, e.g.:

\documentclass{article}
\usepackage[demo]{graphicx}
\usepackage{caption}

\addtolength{\evensidemargin}{1.5cm} 
\addtolength{\textwidth}{-1.5cm}
\addtolength{\marginparwidth}{20pt}
\addtolength{\marginparsep}{10pt}

\captionsetup[marginfigure]{
justification=raggedright,
font=small}

\newcommand\marginfigure[1]{%
  \marginpar{%
    \captionsetup{type=figure,options=marginfigure}%
    #1}}

\begin{document}

text text text ...

\begin{figure}
\centering
\includegraphics{figure1}
\caption{figure 1 text, which I don't want to have a raggedright justification, since it looks quite good without it due to the many words that I can fit into one line in this type of caption.}    
\end{figure}

some 

more 

text 

about 

something

\marginfigure{\includegraphics[width=\marginparwidth]{figure2}
\caption{Here for figure 2, the caption will look odd when few words are stretched across the marginparwidth}}

\end{document}
Related Question