7

There are a bunch of related questions here. But I was not yet able to pull this off yet.

I have the following code: (where hist.tex is a plot drawing)

\fbox{\scalebox{0.40} {\input{hist.tex}}}
\quad
\begin{tabular}{ll}
    p: 78\%&\\\\
    $\mu$: -7\% & $\sigma$: 7\%\\
    $\alpha$: 0.43 & N: 11977
\end{tabular}

The output:

shot
(source: fc.up.pt)

I would like them to be aligned!!

  • 1
    Use [b] position specifier for tabular: ---> \begin{tabular}[b]{ll} – user11232 Aug 7 '13 at 6:25
  • Harish, thx, yours was the simpler solution .. would give you credit if you answered ! – Ricardo Cruz Aug 9 '13 at 2:10
  • Ricardo, It is OK. Glad it helped. :) – user11232 Aug 9 '13 at 2:17
  • @HarishKumar, you should make your comment an answer. I think it's the simplest and thus the best. – Joel Reyes Noche Jan 16 '15 at 4:15
  • @JoelReyesNoche Thanks for the comment but jubobs has already answered in detail. I think it is sufficient. Have a nice day :) – user11232 Jan 16 '15 at 4:19
4

Whether you want both objects aligned at the top or at the bottom, you can use two minipage environments and apply the \vspace{0pt} trick to obtain the desired vertical alignment (a quirk of TeX/LaTeX).

See also Understanding minipages - aligning at top for more details.

Set the optional argument to both minipage environments to

  • t for top alignment,
  • b for bottom alignment.

Note: I've used tikzpicture environment as placeholder for your graph, as you didn't provide the code for that.

enter image description here

\documentclass{article}

\usepackage[demo]{graphicx}
\usepackage{tikz}

\begin{document}

\begin{minipage}[t]{.5\textwidth}
\vspace{0pt}
\raggedleft
    \begin{tikzpicture}
        \filldraw[draw=red,fill=red!20] rectangle (3,3);
    \end{tikzpicture}
\vspace{0pt}
\end{minipage}
\begin{minipage}[t]{.5\textwidth}
\vspace{0pt}
\raggedright
    \begin{tabular}{ll}
            p: 78\%&\\\\
            $\mu$: -7\% & $\sigma$: 7\%\\
            $\alpha$: 0.43 & N: 11977
    \end{tabular}
\vspace{0pt}
\end{minipage}  

\end{document}
  • Hi Jubobs. Sorry for not posting the entire code, but I was just looking for a hint, not the full answer. :) – Ricardo Cruz Aug 7 '13 at 14:25
  • Anyhow, why all the spacing around the figure and the table? alunos.dcc.fc.up.pt/~up200405927/trash/latex2.png (I just wanted to put a figure and a table, as the transition label of a markov chain. I want them to be as compat as possible, because those markov chains can get very large ..) – Ricardo Cruz Aug 7 '13 at 14:27
  • Why the \vspace{0pt}, you mean? It's a trick to get the desired output. See the link in my answer. You can get the graph and the table pretty tight if you apply the code above. Do you need even tighter than that? – jub0bs Aug 7 '13 at 15:04
  • No, no. I mean, if you look at the picture in my comment .. Why is there such a big space around the graph and the table ? That is supposed to be the label of a tikz path, but it's very distant from it .. – Ricardo Cruz Aug 7 '13 at 15:12
  • If you would like to look at the code .. alunos.dcc.fc.up.pt/~up200405927/trash/latex.zip .. The graph & table look very distance from the transition they are associated to. – Ricardo Cruz Aug 7 '13 at 15:15
4

You can use the adjustbox package which does all the work without you setting any length. Just change the mock tikzpicture with your plot.

\documentclass{article}

\usepackage{tikz}
\usepackage{adjustbox}

\begin{document}

\section{Centered alignment}

\begin{adjustbox}{valign=c}
\begin{tikzpicture}
  \filldraw[draw=red,fill=red!20] rectangle (3,3);
\end{tikzpicture}
\end{adjustbox}
\begin{tabular}{ll}
  p: 78\%&\\[2ex]
  $\mu$: -7\% & $\sigma$: 7\%\\
  $\alpha$: 0.43 & N: 11977
\end{tabular}

\section{Top alignment}

\begin{adjustbox}{valign=t}
\begin{tikzpicture}
  \filldraw[draw=red,fill=red!20] rectangle (3,3);
\end{tikzpicture}
\end{adjustbox}
\begin{tabular}[t]{ll}
  p: 78\%&\\[2ex]
  $\mu$: -7\% & $\sigma$: 7\%\\
  $\alpha$: 0.43 & N: 11977
\end{tabular}

\section{Bottom alignment}

%\begin{adjustbox}{valign=b} % adjustbox not really needed
\begin{tikzpicture}
  \filldraw[draw=red,fill=red!20] rectangle (3,3);
\end{tikzpicture}
%\end{adjustbox}
\begin{tabular}[b]{ll}
  p: 78\%&\\[2ex]
  $\mu$: -7\% & $\sigma$: 7\%\\
  $\alpha$: 0.43 & N: 11977
\end{tabular}

\end{document}

enter image description here

1

Depending on whether you wanted it top or center aligned (or bottom for that matter), you would add differing amounts of vertical movement to the image. For my example, moving the top of the image 2 and 3 \baselineskip of upward shift relative to the baseline was sufficient (i.e., negative below-baseline = upward relative to top of image). Your mileage may vary.

\documentclass{article}
\usepackage{graphicx}
\usepackage{stackengine}
\parskip 1em
\begin{document}
\def\x{\fbox{\scalebox{0.40} {\rule{2in}{2in}}}}
\def\y{%
\begin{tabular}{ll}
    p: 78\%&\\
\\
    $\mu$: -7\% & $\sigma$: 7\%\\
    $\alpha$: 0.43 & N: 11977
\end{tabular}}
\belowbaseline[-2\baselineskip]{\x}\quad\y \par
\belowbaseline[-3\baselineskip]{\x}\quad\y
\end{document}

enter image description here

Note that, if you needed a "precise" top alignment, it can be achieved with

\belowbaseline[-\ht\strutbox]{\x}\quad\belowbaseline[-\ht\strutbox]{\y}

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.