4

The following minimal working example does not vertically align two rectangles drawn by TikZ. The result can be found here. Somehow the second rectangle seems bolded, but that is not the case when I open the .pdf on my PC.

I would like the two rectangles to be vertically aligned and centered (which is equivalent to both centering them). Why is the second rectangle indented?

\documentclass[a4paper]{article}

\usepackage{subfig}
\usepackage{tikz,pgfplots}\pgfplotsset{compat = 1.11}

\newcommand{\rect}{%
\begin{tikzpicture}%
\draw (0,0) rectangle (4,4);%
\end{tikzpicture}%
}%

\begin{document}%

\begin{figure}%
\centering%
\subfloat[First rectangle.]{\rect}%
\newline%
\subfloat[Second rectangle.]{\rect}%
\caption{This is a figure with two rectangles that do not horizontally align.}
\end{figure}%

\end{document}
4
  • I just found out that if I replace the following part \subfloat[First rectangle.]{\rect}% \newline% by \subfloat[First rectangle.]{\rect}\\%, then it works. Can someone explain why? – Ritz Jan 28 '15 at 13:33
  • 1
    May be tex.stackexchange.com/a/82666/1952 helps you to understand it – Ignasi Jan 28 '15 at 13:59
  • 1
    Don't use \newline but \par. – user11232 Jan 28 '15 at 14:15
  • Instead of \newline, you should use \par or ` \\ ; \centering` redefines ` \\ ` so it behaves well; however, \newline keeps its definition as \@normalcr\relax (which is essentially the "normal" definition of ` \\ ` ) and this produces bad results. – Gonzalo Medina Jan 28 '15 at 14:33
2

The second rectangle is not indented, the first one is not really centered and this is not due to the subfig package nor to the use of figures created using TikZ.

The problem here is that, inside \centering (\raggedleft and \raggedright and similar), \newline behaves as the "normal" \\ which is basically \@normalcr\relax and this produces undesired behaviours illustrated in the example below. \centering redefines \\ (but \newline keeps its standard definition) to (essentially) \par\addvspace{-\parskip} so when using \centering (\raggedleft and \raggedright and similar) you should use \\ or \par but no \newline:

\documentclass{article}
\usepackage[showframe,a6paper]{geometry}% just for visualization purposes

\begin{document}

\noindent
\begin{minipage}{\linewidth}
\centering
Center \\
Center \par
Center?
\newline 
Center
\end{minipage}

\end{document}

enter image description here

Your example code using \par and \\:

\documentclass[a4paper]{article}
\usepackage{subfig}
\usepackage{tikz}

\newcommand\rect{%
\begin{tikzpicture}
\draw (0,0) rectangle (4,4);
\end{tikzpicture}%
}

\begin{document}

\begin{figure}
\centering
\subfloat[First rectangle.]{\rect}
\par
\subfloat[Second rectangle.]{\rect} 
\\
\subfloat[Third rectangle.]{\rect}
\caption{This is a figure with three rectangles horizontally aligned}
\end{figure}

\end{document}

The output:

enter image description here

4
  • @krnl as I mentioned in my answer, inside \centering, the ` \\ ` command is redefined to be \par plus a possible skip of -\parskip. In standard settings you can use either one. – Gonzalo Medina Jan 29 '15 at 14:43
  • And how can I make the captions as Fig. 1-(a) First rectangle , Fig. 1-(b) Second rectangle and Fig. 1-(c) Third rectangle without the final caption? – AboAmmar Feb 5 '15 at 21:50
  • @AboAmmar Please feel free to open follow-up question. – Gonzalo Medina Feb 6 '15 at 0:15
  • How do you make them underneath each other, but on the left? – ahorn Apr 13 '16 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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