1

I've created a command to streamline my figure creation. I'm working on adding a subcaption (which is really just a parbox) using the subcaption package that will add some vertical text along the right side of my figure (to say the source of the image).

I've got almost everything done, the only problem is that when I rotate the caption, it extends its width to be as long as the text I pass it. This can cause the figure to be exceptionally tall (See example).

What I need is a way to set the width of the rotated subcaptionbox to the height of my image. In my case, I'm setting the width of each image (based on the textwidth), and letting the height of the image follow from the aspect ratio.

Example of figure with long source text

What I have tried: Setting the width of the subcaptionbox doesn't work, I'm guessing that it's reset somehow by the rotatebox? I have also tried vphantom without success. My current code hard sets the width of a parbox, but I need it to adjust to the actual height of the image.

Here is my MWE:

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

\newcommand{\newfigure}[4][]{
    \begin{figure}[h]
        \includegraphics[width=.8\textwidth]{demo} %normally the image, here just the demo
        \subcaptionbox*{}[2cm]{
            \rotatebox[origin=t]{90}{
                \parbox{5cm}{ %Should be set variably to the height of the image
                        \scriptsize{#1}
                }
            }
        }
        \caption{#3}
        \label{#4}
    \end{figure}
}

\begin{document}
        \newfigure[Source: A potially long and protracted source for the image]{Demo image}{Example caption for the figure}{fig:demo}
\end{document}

1 Answer 1

2

Note that \subcaptionbox reserves space for a subcaption below, even if blank.

\documentclass{article}
\usepackage{subcaption}
\usepackage[demo]{graphicx}
\usepackage{showframe}

\newsavebox{\nfbox}

\newcommand{\newfigure}[4][]{
    \begin{figure}[h]
        \savebox\nfbox{\includegraphics[width=.8\textwidth]{demo}}% measure width and height
        \usebox\nfbox
        \subcaptionbox*{}[\dimexpr \columnwidth-\wd\nfbox]{%
            \rotatebox[origin=tc]{90}{%
                \parbox{\ht\nfbox}{ %Should be set variably to the height of the image
                        \scriptsize{#1}%
                }%
            }%
        }%
        \caption{#3}
        \label{#4}
    \end{figure}
}

\begin{document}
        \newfigure[Source: A potially long and protracted source for the image]{Demo image}{Example caption for the figure}{fig:demo}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .