1

I periodically need/want to slightly 'nudge' the placement of a figure, or table, to slightly off-centre (xx em to the left, or xx em to the right). This is easy enough with \hspace{xxem} as shown in the following MWE. But, whilst the figure and/or table itself moves as expected, the caption stays rigidly centered on the page. I can brute force it left or right by (say) padding the left or right end of the caption with as many ~ as needed, but I was wondering if there was a more elegant/robust way of taking the whole of the figure or table -- caption included, and moving the lot of it left or right of centre?

Thanks very much in advance...

 \documentclass[12pt]{article}

 % set up basic graphics
 \usepackage{epsfig,float}

 \begin{document}

  It is easy to `nudge' an image a bit to the left (or right) of center - simply use hspace -- 
  but this doesn't also move the caption: 


 \begin{figure}[H]
  \begin{center}
    \hspace{-5em}\includegraphics[scale=1,keepaspectratio=true]{test.eps}
    \caption{move to left of centre...}
    \end{center}
 \end{figure}

 
 And, same for moving a table left- or right-of-centre -- hspace moves the table, but not the 
caption:

 \begin{table}[H]
 \caption{left-of-center}  
 \centering  
 \hspace{-5em}\begin{tabular}{c c c c}  
 \hline\hline
 Case & Method\#1 & Method\#2 & Method\#3 \\ [0.5ex]  
 \hline 
 1 & 50 & 837 & 970 \\  
 2 & 47 & 877 & 230 \\
 3 & 31 & 25 & 415 \\
 4 & 35 & 144 & 2356 \\
 5 & 45 & 300 & 556 \\ [1ex]  
 \hline  
 \end{tabular}
 \end{table}

 \end{document}
1
  • Off-topic: Why are you loading the ancient epsfig package? You would almost certainly be better off loading the graphicx package...
    – Mico
    Mar 28, 2022 at 18:52

1 Answer 1

1

Update after follow-up question: dealing with long captions.

If the captions are long, they will overflow the margin. To keep the caption within the width of the text and also centered with the image/table, its width should be reduced to the width of the image/title.

For this, a new caption format (longCap) was defined, which will have the legend justified and its width limited to the width of the figure/table, and therefore centered.

In the case of a short caption, the newly defined caption format (shortCap) will keep the caption centered with the figure/table.

b

c

\documentclass[12pt]{article}

% set up basic graphics
\usepackage{float}
\usepackage{graphicx}

\usepackage{caption}% added <<
\usepackage{ifthen}% added <<
\newlength{\nudge}
\newlength{\Cwidth}

\DeclareCaptionFormat{longCap}% long caption > justified
{\ifthenelse{\lengthtest\nudge>0pt}{\setlength{\Cwidth}{\dimexpr\linewidth-\nudge}}{\setlength{\Cwidth}{\dimexpr\linewidth+\nudge}} 
    \hspace*{\nudge}\begin{minipage}{\the\Cwidth} #1#2#3\end{minipage}  
}

\DeclareCaptionFormat{shortCap}% short caption > centered
{\ifthenelse{\lengthtest\nudge>0pt}{\setlength{\Cwidth}{\dimexpr\linewidth-\nudge}}{\setlength{\Cwidth}{\dimexpr\linewidth+\nudge}} 
    \hspace*{\nudge}\begin{minipage}{\the\Cwidth} \centering #1#2#3\end{minipage}   
}

\usepackage{showframe} % ONLY to show thw margins <<<<<<<<<<<<<<<

\begin{document}
    
    It is easy to `nudge' an image a bit to the left (or right) of center - simply use hspace -- 
        
    \section{Long caption => justified} 
    
    \setlength{\nudge}{-6em}        
    \begin{figure}[H]
        \begin{center}
            \hspace{\nudge}
            \includegraphics[scale=1,keepaspectratio=true]{example-image}
            \captionsetup{format=longCap}
            \caption{move to left of centre... As any dedicated reader can clearly see, the Ideal of
                practical reason is a representation of, as far as I know, the things
                in themselves; as I have shown elsewhere, the phenomena should only be
                used as a canon for our understanding.}
        \end{center}
    \end{figure}
    
    \newpage
    
    And, same for moving a table left- or right-of-centre -- hspace moves the table, but not the 
    caption:
    
    \setlength{\nudge}{12em}
    \begin{table}[H]
        \centering 
    \captionsetup{format=longCap}
    \caption{Right-of-center As any dedicated reader can clearly see, the Ideal of
            practical reason is a representation of, as far as I know, the things
            in themselves; as I have shown elsewhere, the phenomena should only be
            used as a canon for our understanding. }         
        \hspace{\nudge}
        \begin{tabular}{c c c c}  
            \hline\hline
            Case & Method\#1 & Method\#2 & Method\#3 \\ [0.5ex]  
            \hline 
            1 & 50 & 837 & 970 \\  
            2 & 47 & 877 & 230 \\
            3 & 31 & 25 & 415 \\
            4 & 35 & 144 & 2356 \\
            5 & 45 & 300 & 556 \\ [1ex]  
            \hline  
        \end{tabular}
    \end{table}

\newpage
\section{Short caption => centered}         
\setlength{\nudge}{-6em}    
\begin{figure}[H]
    \begin{center}
        \hspace{\nudge}
        \includegraphics[scale=1,keepaspectratio=true]{example-image}
        \captionsetup{format=shortCap}
        \caption{move to left of centre... }
    \end{center}
\end{figure}


And, same for moving a table left- or right-of-centre -- hspace moves the table, but not the 
caption:
        
\setlength{\nudge}{12em}
\begin{table}[H]
    \centering 
    \captionsetup{format=shortCap}
    \caption{Right-of-center. }          
    \hspace{\nudge}
    \begin{tabular}{c c c c}  
        \hline\hline
        Case & Method\#1 & Method\#2 & Method\#3 \\ [0.5ex]  
        \hline 
        1 & 50 & 837 & 970 \\  
        2 & 47 & 877 & 230 \\
        3 & 31 & 25 & 415 \\
        4 & 35 & 144 & 2356 \\
        5 & 45 & 300 & 556 \\ [1ex]  
        \hline  
    \end{tabular}
\end{table}

\end{document}
3
  • Perfect -- thank you very much. That does the trick... Mar 28, 2022 at 20:13
  • Turns out the suggested approach (hspace for table, margins for caption) works fine, but becomes a bit of a challenge if the caption is long enough that it breaks over multiple lines. Most of my captions fall into this category...I'll post an example of this when I get a chance. Mar 29, 2022 at 19:26
  • @Johnny Canuck Please see the updated answer with long captions. Mar 30, 2022 at 20:55

You must log in to answer this question.

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