Graphics, Figures & TablesAligning caption to the left side of figure

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
SuzuBell
Posts: 14
Joined: Sat Dec 13, 2014 7:08 am

Aligning caption to the left side of figure

Post by SuzuBell »

I have a figure with a width of 65% of the text aligned to the left of the page. To the right of it, I have a figure with a width of 32% of the text. I would like to place a caption underneath and left-aligned to the left figure with a width of 100% of the text.

I tried it two ways. First, as such:

Code: Select all

\begin{figure}[t]
\captionsetup{width=\textwidth}
\includegraphics[keepaspectratio=TRUE,width=0.65\textwidth]{figure/plotAncDes10.png}
\caption{This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot.}\label{fig:fig5}

\begin{figure}[!t]
\captionsetup{width=\textwidth}
\vspace{27mm}
\includegraphics[keepaspectratio=TRUE,width=0.32\textwidth,right]{figure/repetition5.png}
\end{figure}
\end{figure}

\begin{figure}[!t]
\captionsetup{width=\textwidth}
\vspace{27mm}
\includegraphics[keepaspectratio=TRUE,width=0.32\textwidth,right]{figure/repetition5.png}
\end{figure}
This looks good. The figures are the correct widths and next to each other. The caption seems to be the correct length. However, its alignment is off, and it starts before the left side of the page!

The second way I tried was this:

Code: Select all

\usepackage{graphicx}
\usepackage{caption}
\captionsetup[figure]{slc=off}

\begin{figure}[t]
  \centering
  \captionsetup{width=\textwidth}
  \captionbox
    {This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. \label{fig:fig5}}
  {\includegraphics[keepaspectratio=TRUE,width=0.65\textwidth]{figure/plotAncDes10.png}}
\end{figure}
But this causes the same problem!

Any suggestions will be greatly appreciated!! :o)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Aligning caption to the left side of figure

Post by Johannes_B »

Hi,
i am not sure if i understood you correctly, but nesting figure environments isn't the way to go ;-) Please have a lookt at the following example:

Code: Select all

\documentclass{article}
\usepackage{mwe}
\begin{document}
\begin{figure}
	\begin{minipage}{0.65\textwidth}
		\includegraphics[width=\linewidth,keepaspectratio]{example-image-a}
	\end{minipage}\hfill
	\begin{minipage}{0.32\textwidth}
		\includegraphics[width=\linewidth,keepaspectratio]{example-image-b}
	\end{minipage}
	\caption{This is the caption. \blindtext}
\end{figure}
\blindtext[5]
\end{document}
Hint: Providing fully compilable example (but still as minimal as possible) makes it easier to help. And you can click on »open in writelatex« and play with the code and its output in an instance.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
SuzuBell
Posts: 14
Joined: Sat Dec 13, 2014 7:08 am

Aligning caption to the left side of figure

Post by SuzuBell »

Thank you so much for your advice, Johannes_B.

I did not realize about the "Open in WriteLatex".

When I did "Open in WriteLatex" the code that you wrote, that looks like exactly what I want! However, when I copied the same code into my .Rnw file, the second image appeared below the first image, and the caption was only the width of the text! Also, the top image was near the top of the page, but not really at the top of the page.

As I am using the same code that works in "Open in WriteLatex", is it possible something else in the document is causing the difference? I am pasting in other information I have in my file. I am sorry for the headache!

Code: Select all

% !TeX program = pdfLaTeX
\documentclass[journal]{vgtc} % final (journal style)
\let\ifpdf\relax
\usepackage{mathptmx}
\usepackage{graphicx}
\usepackage{sidecap}
\usepackage{times}
\usepackage{dblfloatfix}
\usepackage{fixltx2e}
\usepackage{mwe}
\usepackage{natbib}
\usepackage{fullpage}
\usepackage{color}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage[bookmarks,backref=true,linkcolor=black]{hyperref} %,colorlinks
\hypersetup{
  pdfauthor = {},
  pdftitle = {},
  pdfsubject = {},
  pdfkeywords = {},
  colorlinks=true,
  linkcolor= black,
  citecolor= black,
  pageanchor=true,
  urlcolor = black,
  plainpages = false,
  linktocpage
}
\usepackage{wrapfig,float}
\usepackage{caption} %%% added
\captionsetup[figure]{slc=off}  %%% added
\usepackage{subcaption}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{url}
\usepackage{ulem}
\usepackage[section]{placeins}
\usepackage[export]{adjustbox}
\usepackage[colorinlistoftodos]{todonotes}
\newcommand{\hh}[1]{{\color{orange} #1}}
\graphicspath{{figure/}}
\renewcommand{\floatpagefraction}{.99}
\newcommand{\done}[2][inline]{\todo[color=SpringGreen, #1]{#2}}
\newcommand{\newdo}[1]{\todo[inline, color=Plum]{#1}}
\newcommand{\comment}[2][inline]{\todo[color=SkyBlue, #1]{#2}}
\newcommand{\newtext}[1]{\todo[inline, color=White]{ \color{OliveGreen}{#1}}}

\begin{document}

\begin{figure}
  \begin{minipage}{0.6\textwidth}
    \includegraphics[width=\linewidth,keepaspectratio]{figure/plotAncDes10.png}
  \end{minipage}\hfill
  \begin{minipage}{0.32\textwidth}
    \includegraphics[width=\linewidth,keepaspectratio]{figure/repetition7.png}
  \end{minipage}
  \caption{This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. \blindtext}
\end{figure}
\blindtext[5]

\end{document}
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Aligning caption to the left side of figure

Post by Johannes_B »

You have a two-column document and in those both column and the little white space between them form the width of the text, i.e. textwidth. You can set the relative size of the picture in relation to the linewidth. I did this above, both images have a width of the full linewidth, which is controlled by the outer minipage. The minipage however was set to take an amount of 60 percent of the textwidth for the first image alone. That is wider than a single column, so the next minipage gets broken to the next line.
So in order to resolve the issue, you should set the outer minipages to use .6 of the linewidth. The line width will be equivalent to the width of a column for a normal figure and equivalent to the text width for a two column figure*
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
SuzuBell
Posts: 14
Joined: Sat Dec 13, 2014 7:08 am

Aligning caption to the left side of figure

Post by SuzuBell »

If I understood your post correctly, you suggested to do this (simply changed the outer minipage to 0.6\linewidth:

Code: Select all

\begin{figure}
  \begin{minipage}{0.6\linewidth}
    \includegraphics[width=\linewidth,keepaspectratio]{figure/plotAncDes10.png}
  \end{minipage}\hfill
  \begin{minipage}{0.32\textwidth}
    \includegraphics[width=\linewidth,keepaspectratio]{figure/repetition7.png}
  \end{minipage}
  \caption{This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. This is the repetition plot. \blindtext}
\end{figure}
\blindtext[5]
Unfortunately, this did not solve the issue. There is still quite a bit of white space before the first figure. Then, the first figure is very small (about 0.6 of the textwidth) and so is the second figure below it. The caption below that is the width of the textwidth.

What I wanted was the first figure to be ~0.6 of linewidth on the left top of the page, the second figure to be ~0.3 of linewidth on the right top of the page, and the caption to be linewidth underneath and aligned to the left side of the first image.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Aligning caption to the left side of figure

Post by Johannes_B »

To be honest, now i am even more confused. Like that?

Code: Select all

\documentclass[twocolumn]{article}
\usepackage{mwe}
\begin{document}
\blindtext[2]
\begin{figure*}
	\begin{minipage}{0.65\linewidth}
		\includegraphics[width=\linewidth,keepaspectratio]{example-image-a}
	\end{minipage}\hfill
	\begin{minipage}{0.32\linewidth}
		\includegraphics[width=\linewidth,keepaspectratio]{example-image-b}
	\end{minipage}
	\caption{This is the caption. \blindtext}
\end{figure*}

\blindtext[5]

\blindtext[2]
\begin{figure}
	\begin{minipage}{0.65\linewidth}
		\includegraphics[width=\linewidth,keepaspectratio]{example-image-a}
	\end{minipage}\hfill
	\begin{minipage}{0.32\linewidth}
		\includegraphics[width=\linewidth,keepaspectratio]{example-image-b}
	\end{minipage}
	\caption{This is the caption. \blindtext}
\end{figure}
\blindtext[5]
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
SuzuBell
Posts: 14
Joined: Sat Dec 13, 2014 7:08 am

Aligning caption to the left side of figure

Post by SuzuBell »

Thanks for your help and patience.

It seems that works for the most part. The only thing that did not work is that the figure is not at the top of the page vertical-wise, it is in the center. Moreover, other figures that used to be at the top of the page are also at the center vertical-wise. I tried to add both [t!] and [!t] after the {figure*} part, but it did not seem to work:

Code: Select all

\documentclass[journal]{vgtc}                % final (journal style)
\let\ifpdf\relax
\usepackage{mathptmx}
\usepackage{graphicx}
\usepackage{sidecap}
\usepackage{times}
\usepackage{dblfloatfix}
\usepackage{fixltx2e}
\usepackage{mwe}
\usepackage{natbib}
\usepackage{fullpage}
\usepackage{color}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage[bookmarks,backref=true,linkcolor=black]{hyperref} %,colorlinks
\hypersetup{
  pdfauthor = {},
  pdftitle = {},
  pdfsubject = {},
  pdfkeywords = {},
  colorlinks=true,
  linkcolor= black,
  citecolor= black,
  pageanchor=true,
  urlcolor = black,
  plainpages = false,
  linktocpage
}
\usepackage{wrapfig,float}
\usepackage{caption} %%% added
\captionsetup[figure]{slc=off}  %%% added
\usepackage{subcaption}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{url}
\usepackage{ulem}
\usepackage[section]{placeins}
\usepackage[export]{adjustbox}
\usepackage[colorinlistoftodos]{todonotes}
\usepackage{fancyhdr} 
\fancyhf{}
\cfoot{\thepage}
\pagestyle{fancy}

\newcommand{\hh}[1]{{\color{orange} #1}}
\graphicspath{{figure/}}
\renewcommand{\floatpagefraction}{.99}
\newcommand{\done}[2][inline]{\todo[color=SpringGreen, #1]{#2}}
\newcommand{\newdo}[1]{\todo[inline, color=Plum]{#1}}
\newcommand{\comment}[2][inline]{\todo[color=SkyBlue, #1]{#2}}
\newcommand{\newtext}[1]{\todo[inline, color=White]{ \color{OliveGreen}{#1}}}

\renewcommand{\headrulewidth}{0pt}
\begin{document} 

% An example of a figure before that used to be at the top of the page, but is not at the center
\begin{figure*}[t!]
\centering
\includegraphics[keepaspectratio=TRUE,width=0.9\textwidth]{figure/plotPathOnTree7.png}
\caption{This is figure number 4. This is figure number 4. This is figure number 4. This is figure number 4. This is figure number 4. This is figure number 4. This is figure number 4. This is figure number 4. This is figure number 4. This is figure number 4. }\label{fig:fig4}
\end{figure*}

\begin{figure*}[!t]
\begin{minipage}{0.65\linewidth}
\includegraphics[width=\linewidth,keepaspectratio]{figure/plotAncDes10.png}
\end{minipage}\hfill
\begin{minipage}{0.32\linewidth}
\includegraphics[width=\linewidth,keepaspectratio]{figure/repetition7.png}
\end{minipage}
\caption{This is the caption. This is the caption. This is the caption. This is the caption. This is the caption. This is the caption. This is the caption. This is the caption. This is the caption.\blindtext}
\end{figure*}

\end{document}
I'm sorry for listing so many questions consecutively like this. I just feel that while I can grasp the basics, things start to become pretty convoluted to me in terms of how certain statements second-handedly affect others :o). Thanks again.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Aligning caption to the left side of figure

Post by Johannes_B »

You can ask as many question as you want, i strongly encourage you to do so. Learning ia a process and it seems you want to learn.
Little hint though: You can enclose your code in

Code: Select all

 [code] to get the nice markup you see above ;-)

You are now talking about float placement i think. Float placement, as great as it may be, can be really tricky. LaTeX calculates the linebreaks of every paragraph and puts floating objects like figures and tables to places where latex sees fit. You can somehow control the movement by using float placement specifiers. The LaTeX default is [latex]tbp[/latex]. An exclamation mark makes LaTeX calculate a bit sloppy, the result won't be as good.
When you add one sentence in the beginning of your document, it is possible that all floats are rearranged. The same can happen if you change the size of a picture so that it takes up a bit more space. 

I cannot give you any specific advice right now, as i don't know your document. But i know this: Concentrate on the content of your document, and as soon as this is done, you can fiddle around with the position of float objects. Might be worth checking with the publisher though. I don't know the requirements, but it seems likely that they will do stuff to your document anyway. You could save yourself some trouble there.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
SuzuBell
Posts: 14
Joined: Sat Dec 13, 2014 7:08 am

Re: Aligning caption to the left side of figure

Post by SuzuBell »

Thank you for your help. I see it can be pretty meticulous :)
Post Reply