9

How can I vertically align a tabular left to a tikz figure? With minipage without a vertical align option it looks good: \begin{minipage}

The problem is that I need it aligned at top (for this I replaced \begin{minipage} with\begin{minipage}[t]) or bottom (\begin{minipage}[b]). In both cases, the tabular is shifted down: \begin{minipage}[t] \begin{minipage}[b Here is my used code:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{xcolor}
\pgfplotsset{compat=1.9}
\definecolor{option1}{HTML}{A6DD5D}
\definecolor{option2}{HTML}{C94F63}
\definecolor{option16}{HTML}{ACA793} %grau
\begin{document}
\begin{minipage}[b]{0.45\textwidth}
\begin{flushright}
\hfill
\begin{tikzpicture}
\begin{axis}[xbar stacked,nodes near coords,
bar width=4mm,
ytick=data,
axis y line*=none,
axis x line*=bottom,
separate axis lines,
y axis line style= { draw opacity=0 },
tick label style={font=\scriptsize},
scaled ticks=false,
legend style={font=\scriptsize},
label style={font=\scriptsize},
xlabel={[\%]},
xmin=0,
xmax=101,
y=5mm,
enlarge y limits={abs=0.625},
width=0.9\textwidth,
yticklabels={15–24, 25–34, 35–44, 45–54, 55–64, über 65},
every node near coord/.append style={font=\scriptsize, /pgf/number format/precision=0, /pgf/number format/fixed}
]
\addplot[fill=option1,draw opacity=0] coordinates
{ (57.8819,0)  (59.0849,1)  (58.2529,2)  (58.0773,3)  (53.3871,4)  (58.2525,5) };
\addplot[fill=option2,draw opacity=0] coordinates
{ (12.584,0)  (16.0601,1)  (19.3631,2)  (22.4693,3)  (27.5239,4)  (22.9074,5) };
\addplot[fill=option16,draw opacity=0] coordinates
{ (29.5341,0)  (24.855,1)  (22.384,2)  (19.4534,3)  (19.089,4)  (18.8401,5) };
\end{axis}
\end{tikzpicture}
\hspace{3mm}
\end{flushright}
\end{minipage}
\begin{minipage}[b]{0.55\textwidth}
\begin{flushleft}
\scriptsize
\begin{tabular}{p{1.6cm}rrrrrr}
& \multicolumn{1}{l}{\textcolor{black}{\textbf{15–24}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{25–34}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{35–44}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{45–54}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{55–64}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{über 65}}}\\
\hline
\textbf{bin dafür} & 58\,\% & 59\,\% & 58\,\% & 58\,\% & 53\,\% & 58\,\%\\
\textbf{bin dagegen} & 13\,\% & 16\,\% & 19\,\% & 22\,\% & 28\,\% & 23\,\%\\
\textbf{weiss nicht/neutral} & 30\,\% & 25\,\% & 22\,\% & 19\,\% & 19\,\% & 19\,\%\\
\end{tabular}
\end{flushleft}
\end{minipage}
\end{document}
  • Did you try [t] on both minipages? – Sigur Jan 16 '14 at 13:54
  • Add vspace{0pt} right below \begin{flushright} and use [t] for minipage. – Jesse Jan 16 '14 at 14:09
  • The problem described above is with [t] on both minipages. – Fabian Heller Jan 17 '14 at 10:32
10

adjustbox is a useful package when you need to align or/and resize graphics or blocks of text. Next example shows how to solve your alignment problem

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{adjustbox}
\pgfplotsset{compat=1.9}
\definecolor{option1}{HTML}{A6DD5D}
\definecolor{option2}{HTML}{C94F63}
\definecolor{option16}{HTML}{ACA793} %grau
\begin{document}
\begin{adjustbox}{valign=t,minipage={.45\textwidth}}
\begin{tikzpicture}
\begin{axis}[xbar stacked,nodes near coords,
bar width=4mm,
ytick=data,
axis y line*=none,
axis x line*=bottom,
separate axis lines,
y axis line style= { draw opacity=0 },
tick label style={font=\scriptsize},
scaled ticks=false,
legend style={font=\scriptsize},
label style={font=\scriptsize},
xlabel={[\%]},
xmin=0,
xmax=101,
y=5mm,
enlarge y limits={abs=0.625},
width=0.9\textwidth,
yticklabels={15–24, 25–34, 35–44, 45–54, 55–64, über 65},
every node near coord/.append style={font=\scriptsize, /pgf/number format/precision=0, /pgf/number format/fixed}
]
\addplot[fill=option1,draw opacity=0] coordinates
{ (57.8819,0)  (59.0849,1)  (58.2529,2)  (58.0773,3)  (53.3871,4)  (58.2525,5) };
\addplot[fill=option2,draw opacity=0] coordinates
{ (12.584,0)  (16.0601,1)  (19.3631,2)  (22.4693,3)  (27.5239,4)  (22.9074,5) };
\addplot[fill=option16,draw opacity=0] coordinates
{ (29.5341,0)  (24.855,1)  (22.384,2)  (19.4534,3)  (19.089,4)  (18.8401,5) };
\end{axis}
\end{tikzpicture}
\end{adjustbox}
\hfill
\begin{adjustbox}{valign=t,minipage={.55\textwidth}}
\scriptsize
\begin{tabular}{p{1.6cm}rrrrrr}
& \multicolumn{1}{l}{\textcolor{black}{\textbf{15–24}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{25–34}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{35–44}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{45–54}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{55–64}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{über 65}}}\\
\hline
\textbf{bin dafür} & 58\,\% & 59\,\% & 58\,\% & 58\,\% & 53\,\% & 58\,\%\\
\textbf{bin dagegen} & 13\,\% & 16\,\% & 19\,\% & 22\,\% & 28\,\% & 23\,\%\\
\textbf{weiss nicht/neutral} & 30\,\% & 25\,\% & 22\,\% & 19\,\% & 19\,\% & 19\,\%\\
\end{tabular}
\end{adjustbox}
\end{document}

enter image description here

6

Two boxes on the same line naturally align on their baselines, so you first need to get the right baseline for the tabular and the tikpicture, then make sure that the boxes you put on the line have those baselines. That last bit is rather easier if you don't nest them in minipage and flushleft which are not doing much anyway.

Here for bottom alignment I use [b] on the tabular and use the (array package) b column type rather than p so that the baseline of the table is the baseline of the last line in the cell in the first column. For top alignment I use [t] on the table anat a p column.

tizpicture has a baseline option and examples show setting the baseline of the whole thing to be the baseline of one of its named nodes but you haven't got any here (I think). The first example just uses the bottom, teh second uses 0 which is (if I read it right) the bottom row of the table an dthe third example uses 25mm which is hopefully 5*5mm and somewhere related to the top row. My control over the tikz alignment is more of a guess than my control over tabular:-)

enter image description here

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{xcolor,array}
\pgfplotsset{compat=1.9}
\definecolor{option1}{HTML}{A6DD5D}
\definecolor{option2}{HTML}{C94F63}
\definecolor{option16}{HTML}{ACA793} %grau
\begin{document}

\begin{flushleft}
\begin{tikzpicture}
\begin{axis}[xbar stacked,nodes near coords,
bar width=4mm,
ytick=data,
axis y line*=none,
axis x line*=bottom,
separate axis lines,
y axis line style= { draw opacity=0 },
tick label style={font=\scriptsize},
scaled ticks=false,
legend style={font=\scriptsize},
label style={font=\scriptsize},
xlabel={[\%]},
xmin=0,
xmax=101,
y=5mm,
enlarge y limits={abs=0.625},
width=0.4\textwidth,
yticklabels={15–24, 25–34, 35–44, 45–54, 55–64, über 65},
every node near coord/.append style={font=\scriptsize, /pgf/number format/precision=0, /pgf/number format/fixed}
]
\addplot[fill=option1,draw opacity=0] coordinates
{ (57.8819,0)  (59.0849,1)  (58.2529,2)  (58.0773,3)  (53.3871,4)  (58.2525,5) };
\addplot[fill=option2,draw opacity=0] coordinates
{ (12.584,0)  (16.0601,1)  (19.3631,2)  (22.4693,3)  (27.5239,4)  (22.9074,5) };
\addplot[fill=option16,draw opacity=0] coordinates
{ (29.5341,0)  (24.855,1)  (22.384,2)  (19.4534,3)  (19.089,4)  (18.8401,5) };
\end{axis}
\end{tikzpicture}
\nolinebreak\hfill 
\scriptsize
\begin{tabular}[b]{b{1.6cm}rrrrrr}
& \multicolumn{1}{l}{\textcolor{black}{\textbf{15–24}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{25–34}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{35–44}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{45–54}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{55–64}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{über 65}}}\\
\hline
\textbf{bin dafür} & 58\,\% & 59\,\% & 58\,\% & 58\,\% & 53\,\% & 58\,\%\\
\textbf{bin dagegen} & 13\,\% & 16\,\% & 19\,\% & 22\,\% & 28\,\% & 23\,\%\\
\textbf{weiss nicht/neutral} & 30\,\% & 25\,\% & 22\,\% & 19\,\% & 19\,\% & 19\,\%\\
\end{tabular}
\end{flushleft}


\begin{flushleft}
\begin{tikzpicture}[baseline=0]
\begin{axis}[xbar stacked,nodes near coords,
bar width=4mm,
ytick=data,
axis y line*=none,
axis x line*=bottom,
separate axis lines,
y axis line style= { draw opacity=0 },
tick label style={font=\scriptsize},
scaled ticks=false,
legend style={font=\scriptsize},
label style={font=\scriptsize},
xlabel={[\%]},
xmin=0,
xmax=101,
y=5mm,
enlarge y limits={abs=0.625},
width=0.4\textwidth,
yticklabels={15–24, 25–34, 35–44, 45–54, 55–64, über 65},
every node near coord/.append style={font=\scriptsize, /pgf/number format/precision=0, /pgf/number format/fixed}
]
\addplot[fill=option1,draw opacity=0] coordinates
{ (57.8819,0)  (59.0849,1)  (58.2529,2)  (58.0773,3)  (53.3871,4)  (58.2525,5) };
\addplot[fill=option2,draw opacity=0] coordinates
{ (12.584,0)  (16.0601,1)  (19.3631,2)  (22.4693,3)  (27.5239,4)  (22.9074,5) };
\addplot[fill=option16,draw opacity=0] coordinates
{ (29.5341,0)  (24.855,1)  (22.384,2)  (19.4534,3)  (19.089,4)  (18.8401,5) };
\end{axis}
\end{tikzpicture}
\nolinebreak\hfill 
\scriptsize
\begin{tabular}[b]{b{1.6cm}rrrrrr}
& \multicolumn{1}{l}{\textcolor{black}{\textbf{15–24}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{25–34}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{35–44}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{45–54}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{55–64}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{über 65}}}\\
\hline
\textbf{bin dafür} & 58\,\% & 59\,\% & 58\,\% & 58\,\% & 53\,\% & 58\,\%\\
\textbf{bin dagegen} & 13\,\% & 16\,\% & 19\,\% & 22\,\% & 28\,\% & 23\,\%\\
\textbf{weiss nicht/neutral} & 30\,\% & 25\,\% & 22\,\% & 19\,\% & 19\,\% & 19\,\%\\
\end{tabular}
\end{flushleft}



\begin{flushleft}
\begin{tikzpicture}[baseline=25mm]
\begin{axis}[xbar stacked,nodes near coords,
bar width=4mm,
ytick=data,
axis y line*=none,
axis x line*=bottom,
separate axis lines,
y axis line style= { draw opacity=0 },
tick label style={font=\scriptsize},
scaled ticks=false,
legend style={font=\scriptsize},
label style={font=\scriptsize},
xlabel={[\%]},
xmin=0,
xmax=101,
y=5mm,
enlarge y limits={abs=0.625},
width=0.4\textwidth,
yticklabels={15–24, 25–34, 35–44, 45–54, 55–64, über 65},
every node near coord/.append style={font=\scriptsize, /pgf/number format/precision=0, /pgf/number format/fixed}
]
\addplot[fill=option1,draw opacity=0] coordinates
{ (57.8819,0)  (59.0849,1)  (58.2529,2)  (58.0773,3)  (53.3871,4)  (58.2525,5) };
\addplot[fill=option2,draw opacity=0] coordinates
{ (12.584,0)  (16.0601,1)  (19.3631,2)  (22.4693,3)  (27.5239,4)  (22.9074,5) };
\addplot[fill=option16,draw opacity=0] coordinates
{ (29.5341,0)  (24.855,1)  (22.384,2)  (19.4534,3)  (19.089,4)  (18.8401,5) };
\end{axis}
\end{tikzpicture}
\nolinebreak\hfill 
\scriptsize
\begin{tabular}[t]{p{1.6cm}rrrrrr}
& \multicolumn{1}{l}{\textcolor{black}{\textbf{15–24}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{25–34}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{35–44}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{45–54}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{55–64}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{über 65}}}\\
\hline
\textbf{bin dafür} & 58\,\% & 59\,\% & 58\,\% & 58\,\% & 53\,\% & 58\,\%\\
\textbf{bin dagegen} & 13\,\% & 16\,\% & 19\,\% & 22\,\% & 28\,\% & 23\,\%\\
\textbf{weiss nicht/neutral} & 30\,\% & 25\,\% & 22\,\% & 19\,\% & 19\,\% & 19\,\%\\
\end{tabular}
\end{flushleft}

\end{document}
3

You can try with [baseline={(0,<y-dimen>)}] option to tikzpicture. Ugly, but works:

\documentclass[a4paper,10pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{xcolor}
\pgfplotsset{compat=1.9}
\definecolor{option1}{HTML}{A6DD5D}
\definecolor{option2}{HTML}{C94F63}
\definecolor{option16}{HTML}{ACA793} %grau
\begin{document}
\begin{minipage}[b]{0.45\textwidth}
\begin{tikzpicture}[baseline={(0,2.1)}]
\begin{axis}[xbar stacked,nodes near coords,
bar width=4mm,
ytick=data,
axis y line*=none,
axis x line*=bottom,
separate axis lines,
y axis line style= { draw opacity=0 },
tick label style={font=\scriptsize},
scaled ticks=false,
legend style={font=\scriptsize},
label style={font=\scriptsize},
xlabel={[\%]},
xmin=0,
xmax=101,
y=5mm,
enlarge y limits={abs=0.625},
width=0.9\textwidth,
yticklabels={15–24, 25–34, 35–44, 45–54, 55–64, über 65},
every node near coord/.append style={font=\scriptsize, /pgf/number format/precision=0, /pgf/number format/fixed}
]
\addplot[fill=option1,draw opacity=0] coordinates
{ (57.8819,0)  (59.0849,1)  (58.2529,2)  (58.0773,3)  (53.3871,4)  (58.2525,5) };
\addplot[fill=option2,draw opacity=0] coordinates
{ (12.584,0)  (16.0601,1)  (19.3631,2)  (22.4693,3)  (27.5239,4)  (22.9074,5) };
\addplot[fill=option16,draw opacity=0] coordinates
{ (29.5341,0)  (24.855,1)  (22.384,2)  (19.4534,3)  (19.089,4)  (18.8401,5) };
\end{axis}
\end{tikzpicture}
\hspace{3mm}
\end{minipage}
\begin{minipage}{0.55\textwidth}
\scriptsize
\begin{tabular}{p{1.6cm}rrrrrr}
& \multicolumn{1}{l}{\textcolor{black}{\textbf{15–24}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{25–34}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{35–44}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{45–54}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{55–64}}} & \multicolumn{1}{l}{\textcolor{black}{\textbf{über 65}}}\\
\hline
\textbf{bin dafür} & 58\,\% & 59\,\% & 58\,\% & 58\,\% & 53\,\% & 58\,\%\\
\textbf{bin dagegen} & 13\,\% & 16\,\% & 19\,\% & 22\,\% & 28\,\% & 23\,\%\\
\textbf{weiss nicht/neutral} & 30\,\% & 25\,\% & 22\,\% & 19\,\% & 19\,\% & 19\,\%\\
\end{tabular}
\end{minipage}
\end{document}

enter image description here

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.