Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Currently I have the following code:

\begin{figure}[htb]
\centering
\subfloat[htb][left]{\fcolorbox{plotBord}{plotPadd}{\includegraphics[scale=0.3]{figs/bla1.png}}}
\hfill
\subfloat[htb][right]{\fcolorbox{plotBord}{plotPadd}{\includegraphics[scale=0.3]{figs/bla2.png}}}
\caption[bla]{foobar}
\end{figure}

Which displays the two images on the left and right end of the text (because of \hfill). If I use something like \hspace{10px} instead, the two images have a certain distance between them in the middle, but the margin to the left and right end of the text width is bigger than the one in the middle. Is there any way to always align two images so that they have the same margins left/middle/right like that:

sketch

The two images always have the same size, but I'll add a border and a certain padding to it by using \fcolorbox{}.

share|improve this question
    
Please always use the "Add Image" icon above the text box to add images. I did this for you now. This uploads them using a special stackexchange imgur.com account which ensures that the image won't be deleted after 6 month. If you do it manually your image might disappear after a while. See the related discussion on Meta.TeX.SX for more information. –  Martin Scharrer Jun 27 '11 at 16:39
    
Okay tanks, didn't see any 'Add image' link because I especially looked for one... But I must have been blind, because I looked for a link and not for an image; now I saw it... Thanks –  tim Jun 27 '11 at 20:09
    
There is also a CTRL+G shortcut for it. –  Martin Scharrer Jun 27 '11 at 20:22
add comment

1 Answer 1

up vote 4 down vote accepted

You can use two more \hfills; the last one requires "something" (\null, which is simply an abbreviation for \hbox{}) to have effect. Another option (see egreg's comment) is to use \hspace{\fill} instead of \hfill and \hspace*{\fill} instead of the last \hfill\null (now, \null is not required). The following example shows both approaches:

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage[demo]{graphicx}
\usepackage{xcolor,subfig}

\colorlet{plotBord}{red}
\colorlet{plotPadd}{blue}
\begin{document}

\begin{figure}[htb]
\centering
\hspace{\fill}%
\subfloat[htb][left]{\fcolorbox{plotBord}{plotPadd}{\includegraphics[scale=0.3]{figs/bla1.png}}}
\hspace{\fill}%
\subfloat[htb][right]{\fcolorbox{plotBord}{plotPadd}{\includegraphics[scale=0.3]{figs/bla2.png}}}
\hspace*{\fill}%
\caption[bla]{foobar}
\end{figure}

\begin{figure}[htb]
\centering
\hfill
\subfloat[htb][left]{\fcolorbox{plotBord}{plotPadd}{\includegraphics[scale=0.3]{figs/bla1.png}}}
\hfill
\subfloat[htb][right]{\fcolorbox{plotBord}{plotPadd}{\includegraphics[scale=0.3]{figs/bla2.png}}}
\hfill\null
\caption[bla]{foobar}
\end{figure}

\end{document}

The demo option for graphicx was only used to make my example compilable for everyone (do not use that option in your actual document). I also defined the colors plotBord and plotPadd. The showframe option for geometry was used to draw a frame for visualization purposes. Only the relevant part is shown in the following image:

enter image description here

share|improve this answer
    
What about \hspace*{\fill} instead of the undocumented \null? –  egreg Jun 27 '11 at 14:45
2  
Sorry to insist, but the last one should be \hspace*{\fill}; try and put a % after it. It's quite interesting to understand why it works without the %. There are also some spurious spaces in your input. –  egreg Jun 27 '11 at 15:11
    
@egreg: I hope this time the example code is OK. I couldn't detect the spurious spaces you mentioned. –  Gonzalo Medina Jun 27 '11 at 15:35
    
You catched those after \hspace{\fill}, but missed the ones after \subfloat[...]{...}. The last one, after \hspace*{\fill} is irrelevant because \end{figure} (or \caption) causes the end of the paragraph and a glob of glue at the end of the paragraph is gobbled anyway (this explains why \hspace{\fill} without * and % works, while \hfill without a \null doesn't). –  egreg Jun 27 '11 at 16:02
    
@egreg: I thought about those spaces after \subfloat[]{} too, but I cannot see any difference in the output whether I use % to remove those spaces or not. Am I misssing something? –  Gonzalo Medina Jun 27 '11 at 16:55
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.