# How can I force a caption to the left in a longtable?

I am using the following suggestion from David

Adding a caption to a graphic inside a longtable

I just want to know how I can make the caption to the left, since I noticed that it kind of changes the left line sometimes.

This is from David. I need to force the caption to the left.

\makeatletter
\def\figcaption{%
\refstepcounter{figure}%
\@dblarg{\@caption{figure}}}
\makeatother

I am using the "figcaption" on my longtable, as stated in the link provided.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\usepackage{makecell,multirow,tabularx,booktabs}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage{caption}
\usepackage[showframe=false,,left=2cm,right=2cm,top=2cm,bottom=4.5cm]{geometry}

%this is for figure captions inside a multicolumn/longtable
\makeatletter
\def\figcaption{%
\refstepcounter{figure}%
\@dblarg{\@caption{figure}}}
\makeatother
%this is for figure captions inside a multicolumn/longtable

\begin{document}
%%%%%%%%%%%%%%%%%%START TABLE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{longtable}{|p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}|}

\multicolumn{6}{r}{\makebox[17cm][r]{\textit{Continuação da página anterior}}}\\ \hline

\hline
\multicolumn{6}{r}{\makebox[17cm][r]{\textit{Continua na próxima página}}}\\
\endfoot
\endlastfoot

\hline
%inicio da figura
\multicolumn{6}{|l|}{\parbox{15cm}{
\includegraphics[scale=0.4]{example-image-a}
\figcaption{This caption should be on the flushleft and it is not}}}
\\ %fim da figura
\multicolumn{6}{|l|}{} \\
\hline
\caption{Table A - test caption forced to the left}
\end{longtable}
\end{document}

• Please, show us what you try so far (in form small, complete document (link is not sufficient). Probably use of caption package can solve your problem. Jul 6 '17 at 18:53
• I changed the initial question. Jul 6 '17 at 19:06
• fine, but this is not complete document. missing is document preamble ... Jul 6 '17 at 19:07
• changed the initial question, @Zarko Jul 6 '17 at 19:14
• When I try to run your code the build hangs. I can't tell if it is an infinite loop or if it is taking forever to download portuguese. Jul 6 '17 at 21:52

Like this?

\documentclass[12pt,a4paper]{article}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=4.5cm]{geometry}% simplified

\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\usepackage{makecell,multirow,tabularx,booktabs}
\usepackage{longtable}
\usepackage{graphicx}
\usepackage{caption}
\captionsetup[table]{singlelinecheck}% added for changing table caption

%this is for figure captions inside a multicolumn/longtable
\makeatletter
\def\figcaption{%
\refstepcounter{figure}%
\@dblarg{\@caption{figure}}
}
\makeatother

\begin{document}
%%%%%%%%%%%%%%%%%%START TABLE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{longtable}{|p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}p{2.5cm}|}

\multicolumn{6}{r}{\textit{Continuação da página anterior}}\\ \hline
\hline
\multicolumn{6}{r}{\textit{Continua na próxima página}}\\
\endfoot
\endlastfoot
\hline
%inicio da figura
\multicolumn{6}{|l|}{
\includegraphics[scale=0.4]{example-image-a}
\figcaption{This caption should be on the flushleft and it is not}}
\\ %fim da figura
\multicolumn{6}{|l|}{} \\
\hline
\caption{Table A - test caption forced to the left}
\end{longtable}
\end{document}

Edit: For captions are defined two set-ups, one for all caption, one width addition for tables' captions. The result of MWE is the same as before.

• Thanks for the answer. Yes, that is what I want but it didn't work for me. I copied the captionsetup and added to my code and it does not show to the left. Jul 6 '17 at 19:41
• try to move the first \captionsetup{singlelinecheck=false} imidiatle after \begin{longtable}{...}. Jul 6 '17 at 19:49
• Thanks for the answer but it did not make any difference. I edited the question to match your suggestion and added a figure to show the results afterward. Jul 6 '17 at 19:58
• I was in the impression that we might fix this by changing the macro defined by David but I couldn't make it. Jul 6 '17 at 20:28
• I check the code again. My proposition to move \captionsetup{singlelinecheck=false} inside long table was false. MWE in my answer work as expected. I edit it slightly (add comments to code). Jul 6 '17 at 21:16

This answer uses some of the capabilities of the caption and subcaption packages. It may provide a versatile solution since a range of customizations become possible. I also tried to use some of the capabilities of the ltcaption package, but in the end, none are actually implemented here.

For simplicity, I removed code not relevant to formatting the captions and subcaptions. The caption, ltcaption and subcaption packages make available their respective \captionsetup, \captionsetup[longtable] and \captionsetup[sub] macros, but as already mentioned, in the end I did not use \captionsetup[longtable].

The figures are placed in subfigure environments, which are actually minipages. Use of the subfigure environment is a requirement for using \subcaption. The subcaptions then utilize the formatting provided by \captionsetup[sub]. Since this specifies justification=raggedright the captions for the subfigures are set at the left margin. The longtable caption is also set at the left margin using the captionsetup approach, but in this case it is more likely that the requirement is for the caption to be left aligned with the edge of the table so I added margin=1.75 to achieve that. The end result is that it appears to be close to the requirement.

It is also possible to customize the formatting of the captions to provide the colon as the separator between the caption and the caption type (in this case Table and Subfigure). Those names too can be customized using the \DeclareCaptionLabelFormat macro. I added label formatting setups for the table caption, written in Portuguese as Tabela and for subfigure formatting, I just used Subfigure. The example posted indicates that subfigures have Arabic numbering, but my attempt to reset the numbering from alpha from Arabic did not work. The example also shows the longtable caption with an alpha label, which is achieved with \renewcommand{\thetable}{\Alph{table}}.

The result is this:

The MWE is as follows:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage[portuguese]{babel}
\usepackage[T1]{fontenc}
\usepackage{longtable,ltcaption,subcaption}
\usepackage{graphicx}
\usepackage[showframe=false,left=2cm,right=2cm,top=2cm,bottom=4.5cm]{geometry}

%\LTcapmarginsfalse % for testing
\setlength\LTcapleft{0pt} % Not working?
\setlength\LTcapright{0pt} % Not working?
\DeclareCaptionLabelFormat{sfcapt}{Subfigure {#2}}
\DeclareCaptionLabelFormat{ltcapt}{Tablea {#2}}
\captionsetup[sub]{labelformat=sfcapt,labelsep=colon,justification=raggedright,singlelinecheck=off}
%\captionsetup[longtable]{labelformat=ltcapt,labelsep=colon,justification=raggedright,singlelinecheck=off,width=1.0\linewidth} %justification not working?
\captionsetup{labelformat=ltcapt,margin=1.75cm,labelsep=colon,justification=raggedright,singlelinecheck=off}
\renewcommand{\thetable}{\Alph{table}}
\renewcommand{\thesubfigure}{\arabic{subfigure}} % Not working?
\setcounter{subfigure}{1} % Initialize the subfigure counter

\begin{document}
%%%%%%%%%%%%%%%%%%START TABLE%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{table}[ht]
\begin{longtable}{|p{3cm}||p{3cm}||p{3cm}||p{3cm}|}
\hline
\begin{subfigure}[t]{1.0\linewidth}
\includegraphics[scale=0.25]{example-image-a} \\
\subcaption{This caption should be on the flushleft and it is not}
\end{subfigure}
&
\begin{subfigure}[t]{1.0\linewidth}
\includegraphics[scale=0.25]{example-image-a} \\
\subcaption{This caption should be on the flushleft and it is not}
\end{subfigure}
&
\begin{subfigure}[t]{1.0\linewidth}