Graphics, Figures & TablesSubfig, overpic & caption: position caption labels over imgs

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Subfig, overpic & caption: position caption labels over imgs

Post by sdaau »

Hi all,

I am using the code, included below to position three images in a single figure using subfig. The positioning is as I want it (see image), but I would also like to position the "caption labels" (don't know how else to call them) over the bottom right corner (rel. position 90,10 via overpic) of each image.
imgtestlatex.png
imgtestlatex.png (105.07 KiB) Viewed 18937 times
I tried using the overpic package for that, and so tried tucking the \label command in there; but as you can see that is ignored - furthermore, the reference to the first label is lost (the code is composed from several sources, and I simply ripped the trick for savebox from subfig.pdf). I also tried adding test characters (capital letters A, B and C) and they are rendered properly.

Would anyone know how to place the "caption labels" (a), (b) and (c) where currently A, B and C stand?

And while we're at it, my secondary question - given (a), (b) and (c) can be placed over the bottom right corner, how could I get say a white background behind the text?

Thanks,
Cheers !

Code: Select all

\begin{document}

\newsavebox{\lefttempbox}
	 
\begin{figure}%
	\centering  % no centering, else images will not be next to each other, but ... 
	\begin{minipage}[t]{0.9\textwidth} % ... add one more minipage to have all images centered  
	
		% this savebox will specify height of the whole figure
		\sbox{\lefttempbox}{\begin{overpic}[grid,width=0.55\textwidth]{img001.png}
			\put(90,10){A\protect\label{fig:ex3-a}}
		\end{overpic}}%
		
		% add the saved box as leftmost image
		\subfloat[][]{\usebox{\lefttempbox}}%
		\qquad % leave some horizontal space
		%
		% add minipage too, to control vertical alignment. fixes width too - so images can refer to \textwidth
		\begin{minipage}[t]{0.35\textwidth} 
			\vbox to \ht\lefttempbox{%
				% add top image
				\subfloat[][]{%
					\begin{overpic}[grid,width=\textwidth]{img002.png}
					\put(90,10){B\label{fig:ex3-b}}
					\end{overpic}
				}%
				%
				\vfil % leave vertical space
				%
				% add bottom image
				\subfloat[][]{%
					\begin{overpic}[grid,width=\textwidth]{img003.png}
					\put(90,10){C\label{fig:ex3-c}}
					\end{overpic}
				}%	  
			} % end vbox
		\end{minipage}
		%
		\caption{Three more sub-floats: \subref{fig:ex3-a} , \subref{fig:ex3-b}, \subref{fig:ex3-c}  }
		\label{fig:ex3}
	\end{minipage}
\end{figure}

Continue text...
\end{document}

Last edited by sdaau on Fri Feb 19, 2010 7:24 pm, edited 1 time in total.

Recommended reading 2024:

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

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

sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Subfig, overpic & caption: position caption labels over imgs

Post by sdaau »

Ok, did some more syntax experimenting, and learned a bit more - though it does not look promising (see for corresponding code below):
imgtestlatex02.png
imgtestlatex02.png (101.03 KiB) Viewed 18930 times
After closer reading of subfig.pdf, it turns out that these subcaptions are in fact handled by 'caption' package. As such, I can set their relative position using captionsetup - but that then messes (slightly) the position of the main caption.

Then, I found that \colorbox can provide a background as I want, and works great withing overpic - unfortunately not for automatic caption labels.

So, although I don't see many options, I really hope someone more knowledgeable will point out that what I want to achieve is indeed possible (without necesarilly programming a new package :) )

Thanks,
Cheers!

Code: Select all

\begin{document}

\newsavebox{\lefttempbox}
	 
\begin{figure}%
	\centering  % no centering, else images will not be next to each other, but ... 
	\begin{minipage}[t]{0.9\textwidth} % ... add one more minipage to have all images centered  
		%% \captionsetup[subfloat]{ options } - options apply only to all subfloats? but also influences the figure caption too
		% but captionsetup seems to have full responsibility for placement of subcaptions
		% negative captionskip pulls them up, and margin pulls them "in" from both left and right
		% without singlelinecheck, none of this is active, though
		\captionsetup[subfloat]{captionskip=-1.2em,margin=5pt,justification=raggedleft,singlelinecheck=false,font=normalsize}
		
		% this savebox will specify height of the whole figure
		\sbox{\lefttempbox}{\begin{overpic}[grid,width=0.55\textwidth]{img001.png}
			\put(90,10){\colorbox{red}{A\label{fig:ex3-a}}}
		\end{overpic}}%
		
		% add the saved box as leftmost image
		\subfloat[][]{\usebox{\lefttempbox}}% % subfloat caption actually determined here!
		\qquad % leave some horizontal space
		%
		% add minipage too, to control vertical alignment. fixes width too - so images can refer to \textwidth
		\begin{minipage}[t]{0.35\textwidth} 
			\vbox to \ht\lefttempbox{%
				% add top image - test without overpic - coordinates of put are different here
				\subfloat[][]{%
					\includegraphics[width=\textwidth]{img002.png}
					\put(0,0){\colorbox{red}{B\label{fig:ex3-b}}}
				}%
				%
				\vfil % leave vertical space
				%
				% add bottom image
				\subfloat[][]{%
					\begin{overpic}[grid,width=\textwidth]{img003.png}
					\put(90,10){C\label{fig:ex3-c}}
					\end{overpic}
				}%	  
			} % end vbox
		\end{minipage}
		%
	\end{minipage}
	%\captionsetup{captionskip=2em,justification=centerlast}
	\clearcaptionsetup{figure} % cannot do subfloat
	\caption{Three more sub-floats: \subref{fig:ex3-a} , \subref{fig:ex3-b}, \subref{fig:ex3-c}  }
	\label{fig:ex3}	
\end{figure}

Continue text...
\end{document}

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Subfig, overpic & caption: position caption labels over imgs

Post by localghost »

I suggest to use the subcaption package that comes with caption. It has an interface that is much easier to use and in opposite to subfig is regularly maintained.


Best regards and welcome to the board
Thorsten
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Subfig, overpic & caption: position caption labels over imgs

Post by sdaau »

Ok, problem solved (I think :) )
imgtestlatex03.png
imgtestlatex03.png (98.34 KiB) Viewed 18926 times
(examples on image/code deliberately made all different, so as to compare different approaches)

First crucial piece of info (that I was lucky to stumble upon):
http://www.latex-community.org/forum/viewtopic.php?f=4&t=1730 wrote: LaTeX Community • View topic - Disable hypertext links in hyperref!?
localghost wrote:
wheely_chairs wrote:[...] the subcaption package!? [...]
Since Version 3.1 this package is a part of caption and represents some kind of replacement for subfig, which seems not to be maintained anymore. It has its own documentation where you can find useful information.
Note that:
  • You cannot use both \usepackage{subcaption} and \usepackage{subfig}, they conflict
  • Whichever you use, subcaption or subfig, they must be "included" after \usepackage{caption}
  • subcaption actually understands the \subfloat command, so you can avoid using subfig, while still keeping your \subfloat code.
After starting to use subcaption (instead of subfig) immediately the problem with the reference to the first label dissapeared.

Second crucial piece of info:
http://www.lncc.br/~jmsilva/pdf/caption-eng.pdf wrote: Customizing captions of floating environments using the caption package

Likewise you can define your own caption label formats:
\DeclareCaption-
LabelFormat
\DeclareCaptionLabelFormat{(name)}{(code using #1 and #2)}
At usage #1 gets replaced with the name (e.g. “figure”) and #2 gets replaced with the
reference number (e.g. “12”). An example:
\DeclareCaptionLabelFormat{bf-parens}{(\textbf{#2})}
\captionsetup{labelformat=bf-parens,labelsep=quad}
... Which is, more or less, exactly what I did in the code below (sorry, the comments got a bit messier).

Well, hope this helps someone,
Cheers!

Code: Select all


% subfig and subcaption conflict!
\usepackage[font=footnotesize,labelfont=bf]{caption}  %many ways to customise the captions - text justification
\usepackage{subcaption} % so we can modify subcaptions - understands subfloat command
%\usepackage[caption=false]{subfig} %subfigure is deprecated, use subfig (which is not backwards compatible). Must be included after caption. But even "[subcaption] package is a part of caption and represents some kind of replacement for subfig, which seems not to be maintained anymore."

\DeclareCaptionLabelFormat{myclf}{\colorbox{red}{(\textsl{#2})}}


\begin{document}

\newsavebox{\lefttempbox}
	 
\begin{figure}%
	\centering  % no centering, else images will not be next to each other, but ... 
	\begin{minipage}[t]{0.9\textwidth} % ... add one more minipage to have all images centered  
		% \captionsetup[subfloat]{ options } - options apply only to all subfloats? but also influences the figure caption too
		% but captionsetup seems to have full responsibility for placement of subcaptions
		% negative captionskip pulls them up, and margin pulls them "in" from both left and right
		% without singlelinecheck, none of this is active, though
		
		% captionskip makes a problem with subcaption; but subcaption knows subfloat
		%\captionsetup[subfloat]{captionskip=-1.2em,margin=5pt,justification=raggedleft,singlelinecheck=false,font=normalsize} 
		% this works for color background of label only
		\captionsetup[subfloat]{labelformat=myclf,labelsep=quad}
		
		% this savebox will specify height of the whole figure
		\sbox{\lefttempbox}{\begin{overpic}[grid,width=0.55\textwidth]{img001.png}
			\put(90,10){\subcaptionbox{\colorbox{red}{A\label{fig:ex3-a}}}}
		\end{overpic}}%
		
		% add the saved box as leftmost image - [][]
		% do not call \subfloat[][] when using subcaption and \subcaptionbox - sublabels will be included twice!
		\subfloat{\usebox{\lefttempbox}}% % subfloat caption actually determined here!
		\qquad % leave some horizontal space
		%
		% add minipage too, to control vertical alignment. fixes width too - so images can refer to \textwidth
		\begin{minipage}[t]{0.35\textwidth} 
			\vbox to \ht\lefttempbox{%
				% add top image - test without overpic - coordinates of put are different here
				\subfloat{%
					\includegraphics[width=\textwidth]{img002.png}
					 \put(0,0){\subcaptionbox{\colorbox{red}{B\label{fig:ex3-b}}}} 
				}%
				%
				\vfil % leave vertical space
				%
				% add bottom image
				\subfloat{%
					\begin{overpic}[grid,width=\textwidth]{img003.png}
					\put(90,10){\subcaptionbox{C\label{fig:ex3-c}}}
					\end{overpic}
				}%	  
			} % end vbox
		\end{minipage}
		%
	\end{minipage}
	%\captionsetup{captionskip=2em,justification=centerlast}
	\clearcaptionsetup[subfloat]{} % {figure}, cannot do [subfloat] without {}
	\caption{Three more sub-floats: \subref{fig:ex3-a} , \subref{fig:ex3-b}, \subref{fig:ex3-c}  }
	\label{fig:ex3}	
\end{figure}

Continue text...
\end{document}

sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Subfig, overpic & caption: position caption labels over imgs

Post by sdaau »

Hi localghost,

Thanks for the response and for the welcome :)
localghost wrote:I suggest to use the subcaption package that comes with caption. It has an interface that is much easier to use and in opposite to subfig is regularly maintained.
Hah ! Just as I posted my solution which is based on exactly the same advice :D Nice synchonicity :)

Cheers !
sdaau
Posts: 26
Joined: Fri Feb 19, 2010 2:08 pm

Subfig, overpic & caption: position caption labels over imgs

Post by sdaau »

Hmm... seems I prematurely declared victory earlier :)

First, there is a problem with the labels - the generated ones are a, c, e - b and d are skipped; the subcaption.pdf says: "The star variant is neither incrementing the sub-caption counter nor setting an hyperref anchor.", and that solves the matter.

The other thing is, the earlier code uses \usepackage[rel]{overpic}; but in order to align the boxes properly for all three images, \usepackage[abs]{overpic} should be used; and then the placement coordinates should be calculated for each image according to its width. While the width can be obtained pretty easily, it is in fact a string with "pt" at the end, which \put crashes on. So, now we basically have to take a substring in Latex, which seems not the easiest thing to do, but there is this "catcode" trick from LaTeX Community • View topic - How to print out the value of \textwidth? which helped.

Those changes taken into account, the code is below.

Well, hope there are no further 'big' bugs with this,

Cheers!

Code: Select all


\usepackage{color} %enables text in color: \textcolor

% subfig and subcaption conflict!
\usepackage[font=footnotesize,labelfont=bf]{caption}  %many ways to customise the captions - text justification
\usepackage{subcaption} % so we can modify subcaptions - understands subfloat command
%\usepackage[caption=false]{subfig} %subfigure is deprecated, use subfig (which is not backwards compatible). Must be included after caption. But even "[subcaption] package is a part of caption and represents some kind of replacement for subfig, which seems not to be maintained anymore."
%\usepackage{subfigure} %provides support for the inclusion of small, 'sub'- figures and tables.
\usepackage[abs]{overpic} % Combine LaTeX commands over included graphics. Use abs instead of percent, then can scale via \textwidth

% corner image label
\definecolor{cornerlabelbckgcol}{gray}{0.8}
% http://74.125.77.132/search?q=cache:X7wj_-tm-NUJ:www.lncc.br/~jmsilva/pdf/caption-eng.pdf
\DeclareCaptionLabelFormat{cornerlabelbckg}{\colorbox{cornerlabelbckgcol}{(\textsl{#2})}}
% 
% for proper calc of position of corner image label with [abs]{overpic}:
% this catcode trick can erase "pt" at end of textwidth
% http://www.latex-community.org/forum/viewtopic.php?f=5&t=2712
% since it erases "p" and "t", the defname (here ersz) cannot contain "p" nor "t"! 
% (else would've called it 'erspt')
{\catcode`p=12 \catcode`t=12 \gdef\ersz#1pt{#1}}
\newcommand{\textwidthplain}{\expandafter\ersz\the\textwidth } %textwidth without 'pt' on end
% so can use, to store only number without pt: \renewcommand{\textwidthplain}{\expandafter\ers\the\textwidth } 
% and to printout: \textwidthplain - \the\textwidth ... or \message{HERE IS \textwidthplain}
\newlength{\cornerw} % variable for placement of cornel label (used for xput)
\newlength{\cornerh} % variable for placement of cornel label (used for yput)
\newlength{\cornerleft} % variable for placement of cornel label (used for xput)



\begin{document}

\newsavebox{\lefttempbox}
% \put expects "pure" numbers - will crash if there is "pt" on end...
\newcommand{\xput}{90}
\newcommand{\yput}{20}
\setlength{\cornerleft}{-1.85em}

\begin{figure}%
	\centering  % no centering, else images will not be next to each other, but ... 
	\begin{minipage}[t]{0.9\textwidth} % ... add one more minipage to have all images centered  

		% this works for color background of label only
		\captionsetup[subfloat]{labelformat=cornerlabelbckg,labelsep=quad}
		
		% renew yput here after captionsetup, so char size is correct
		% all three images will use it the same
		\setlength{\cornerh}{1.5\baselineskip}
		\renewcommand{\yput}{\expandafter\ersz\the\cornerh }
		
		\setlength{\cornerw}{0.55\textwidth} % set to scaled width
		
		% this savebox will specify height of the whole figure
		\sbox{\lefttempbox}{%
		\begin{overpic}[grid,width=\cornerw,unit=1pt]{img001.png}
			\addtolength{\cornerw}{\cornerleft} % go back left, leave space for the character
			\renewcommand{\xput}{\expandafter\ersz\the\cornerw } % get myw as number only 	
			\put(\xput,\yput){\subcaptionbox{\label{fig:ex3-a}}}
		\end{overpic}}%
		
		% add the saved box as leftmost image
		% do not call \subfloat[][] when using subcaption and \subcaptionbox - sublabels will be included twice!
		% but - although they are not shown, they are still skipped (i.e. am getting a,c,e)
		% so, use: "The star variant is neither incrementing the sub-caption counter nor setting an hyperref anchor."
		\subfloat*{\usebox{\lefttempbox}}% % subfloat caption actually determined here!
		\qquad % leave some horizontal space
		%
		\setlength{\cornerw}{0.35\textwidth} % set to scaled width
		% add minipage too, to control vertical alignment. fixes width too - so images can refer to \textwidth
		\begin{minipage}[t]{\cornerw} 
			\vbox to \ht\lefttempbox{%
				% overpics don't use myw here, so ok to subtract immediately
				\addtolength{\cornerw}{\cornerleft} % go back left, leave space for the character
				\renewcommand{\xput}{\expandafter\ersz\the\cornerw } % get myw as number only 
				%
				% add top image - test without overpic - coordinates of put are different here
				\subfloat*{%
					\begin{overpic}[grid,width=\textwidth,unit=1pt]{img002.png}
					\put(\xput,\yput){\subcaptionbox{\label{fig:ex3-b}}}
					\end{overpic}
				}%
				%
				\vfil % leave vertical space
				%
				% add bottom image
				\subfloat*{%
					\begin{overpic}[grid,width=\textwidth,unit=1pt]{img003.png}
					\put(\xput,\yput){\subcaptionbox{\label{fig:ex3-c}}}
					\end{overpic}
				}%	  
			} % end vbox
		\end{minipage}
		%
	\end{minipage}
	%
	\clearcaptionsetup[subfloat]{} % {figure}, cannot do [subfloat]
	\caption{Three more sub-floats: \subref{fig:ex3-a} , \subref{fig:ex3-b}, \subref{fig:ex3-c}   }
	\label{fig:ex3}	
\end{figure}

Continue text...

% some debug messages
%\message{HERE IS \the\textwidth} %"134.4407pt"
%\message{HERE IS \uselengthunit{PT} \printlength{\textwidth}} %\usepackage{printlen}
%\message{HERE IS \printinunitsof{cm}\prntlen{\textwidth} } %\usepackage{layouts}
%\renewcommand{\textwidthplain}{\expandafter\ersz\the\textwidth } 
%\message{HERE IS \textwidthplain}
\end{document}

User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

Subfig, overpic & caption: position caption labels over imgs

Post by sommerfee »

Just a little annotate:
sdaau wrote:

Code: Select all

\subcaptionbox{\colorbox{red}{A\label{fig:ex3-a}}}
As opposite to the \subfloat command the \subcaptionbox command has TWO mandatory arguments, one for the sub-caption and one for the contents. So I wonder why the code above works fine...

Code: Select all

\clearcaptionsetup[subfloat]{} % {figure}, cannot do [subfloat]
If you use \captionsetup inside an environment, its scope is limited to the environment only. So this \clearcaptionsetup does not make any harm, but it's superfluous.

Axel
Post Reply