4

I need to put my tikzpicture and text in the same height. However, I cannot do it. This is my code:

\begin{table}
\centering
\begin{tabularx}{\textwidth}{ |l|X| }
\hline
 \multicolumn{1}{c|}{
 \begin{tabular}[c]{@{}c@{}}   
  \begin{tikzpicture}
    \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
    \draw [blue,very thick](0,0) rectangle (4,0.5);
    \draw[thick,<->] (0,-0.2) -- (4,-0.2);
    \node at (2,-0.5) {$w/2$};
    \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
    \draw[thick,<->] (0,1.5) -- (1,1.3);
    \node at (0.5,1.8) {$R$};
    \draw[very thick,->, red] (0,1.5) -- (0,0.9);
    \node[red] at (-0.2,1.2) {$\delta$};
    \draw[thick,<->] (4.2,0) -- (4.2,0.5);
    \node at (4.5,0.25) {$h$};
    \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
    \draw[thick,dashed,->] (0,0) -- (0,2.2) node[anchor=south east] {y};
   \end{tikzpicture} \\$h=1$\;\;$R=4.366h$\;\;$w/2=20h$
   \end{tabular}}
& very very long text........
   \end{tabularx}
   \end{table}

The table shows like this:enter image description here What I want is to place the text at the same height as my picture, I don't want the blank at the top. Can anyone help me??? Thanks a lot!

2
  • Welcome to TeX.SX! Please help us to help you and complete your code to form a minimal working example (MWE). We want something we can copy-paste-compile to work with!
    – cfr
    Commented Jan 3, 2015 at 19:16
  • you have aligned the centre of the paragraph with the baseline of the picture, you probably would prefer top aligned or both centre or both bottom. Commented Jan 3, 2015 at 19:28

2 Answers 2

6

This solution doesn't require any additional packages. Instead, it modifies the optional argument of tabular along the lines suggested in David Carlisle's comment, and makes use of the baseline key when setting up the tikzpicture. The use of baseline is adapted from the TiKZ manual, around page 125.

Corrections required to construct usable MWE before thinking about answering the question:

  • complete code with \documentclass etc. to create starting example;
  • add tabularx, tikz as needed packages, and kantlipsum just for easy long text;
  • add required TiKZ library, patterns, as LaTeX complained it couldn't compile without it;
  • correct code to enable it to compile by putting maths commands inside a maths environment (\: needed to be inside $...$ - not outside).

Changes made in answering the question:

  • \begin{tabular}[c]... says to vertically centre the tabular on the current baseline, which you don't want...

    • \begin{tabular}[t]... aligns the top line with the current baseline, which I think is what you want...
  • tikzpicture has a default alignment set so that the bottom of the picture is aligned with the current baseline, which combines with the c alignment of tabular to centre the picture with respect to the first line of text on the right... which is not what you want...

    • baseline can be used to adjust the alignment of tikzpicture e.g. I've used a named node so that that node's baseline will be aligned with the current baseline and, combining this with the t option for tabular, this aligns that node with the first line of text on the right... which may be closer to what you want...

    • y is the named node, with the name being attached to the y label on the axis, which seems to be the top-most node in the diagram.

top aligned to y node

\documentclass{article}
\usepackage{kantlipsum, tabularx, tikz}
\usetikzlibrary{patterns}
\begin{document}
  \begin{table}
    \centering
    \begin{tabularx}{\textwidth}{ |l|X| }
      \hline
      \multicolumn{1}{|c|}{
        \begin{tabular}[t]{@{}c@{}}
          \begin{tikzpicture}[baseline=(y)]
            \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
            \draw [blue,very thick](0,0) rectangle (4,0.5);
            \draw[thick,<->] (0,-0.2) -- (4,-0.2);
            \node at (2,-0.5) {$w/2$};
            \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
            \draw[thick,<->] (0,1.5) -- (1,1.3);
            \node at (0.5,1.8) {$R$};
            \draw[very thick,->, red] (0,1.5) -- (0,0.9);
            \node[red] at (-0.2,1.2) {$\delta$};
            \draw[thick,<->] (4.2,0) -- (4.2,0.5);
            \node at (4.5,0.25) {$h$};
            \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
            \draw[thick,dashed,->] (0,0) -- (0,2.2) node (y) [anchor=south east] {y};
          \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
        \end{tabular}}
      & \kant[1]\\\hline
    \end{tabularx}
  \end{table}
\end{document}

An alternative way to achieve the same result is to use

  • \begin{tabular}[t]...
  • \begin{tikzpicuter}[baseline=(current bounding box.north)

If you would prefer the columns to be centred vertically, you could use a vertically centred \parbox for the text in the right column.

  \begin{table}
    \centering
    \begin{tabularx}{\textwidth}{ |l|X| }
      \hline
      \multicolumn{1}{|c|}{
        \begin{tabular}[c]{@{}c@{}}
          \begin{tikzpicture}[baseline=(current bounding box.center)]
            \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
            \draw [blue,very thick](0,0) rectangle (4,0.5);
            \draw[thick,<->] (0,-0.2) -- (4,-0.2);
            \node at (2,-0.5) {$w/2$};
            \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
            \draw[thick,<->] (0,1.5) -- (1,1.3);
            \node at (0.5,1.8) {$R$};
            \draw[very thick,->, red] (0,1.5) -- (0,0.9);
            \node[red] at (-0.2,1.2) {$\delta$};
            \draw[thick,<->] (4.2,0) -- (4.2,0.5);
            \node at (4.5,0.25) {$h$};
            \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
            \draw[thick,dashed,->] (0,0) -- (0,2.2) node [anchor=south east] {y};
          \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
        \end{tabular}}
      & \parbox[c]{\linewidth}{\kant[1]}\\\hline
    \end{tabularx}
  \end{table}

centred columns

However, I would also strongly recommend taking a look at the booktabs package which is designed for typesetting professional-quality tables. In particular, it enhances the spacing and offers more flexible rules. One of the recommendations is to drop vertical rules completely, and to use slightly thicker horizontal rules to start and end the tabular.

tabular with <code>booktabs</code>

\documentclass{article}
\usepackage{kantlipsum, tabularx, tikz, booktabs}
\usetikzlibrary{patterns}
\begin{document}
  \begin{table}
    \centering
    \begin{tabularx}{\textwidth}{ lX }
      \toprule
      \multicolumn{1}{c}{
        \begin{tabular}[c]{@{}c@{}}
          \begin{tikzpicture}[baseline=(current bounding box.center)]
            \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
            \draw [blue,very thick](0,0) rectangle (4,0.5);
            \draw[thick,<->] (0,-0.2) -- (4,-0.2);
            \node at (2,-0.5) {$w/2$};
            \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
            \draw[thick,<->] (0,1.5) -- (1,1.3);
            \node at (0.5,1.8) {$R$};
            \draw[very thick,->, red] (0,1.5) -- (0,0.9);
            \node[red] at (-0.2,1.2) {$\delta$};
            \draw[thick,<->] (4.2,0) -- (4.2,0.5);
            \node at (4.5,0.25) {$h$};
            \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
            \draw[thick,dashed,->] (0,0) -- (0,2.2) node [anchor=south east] {y};
          \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
        \end{tabular}}
      & \parbox[c]{\linewidth}{\kant[1]}\\\bottomrule
    \end{tabularx}
  \end{table}
\end{document}
3
  • Thanks a lot! You're right! I need to complete the code for you to better help me!
    – Windy
    Commented Jan 3, 2015 at 22:48
  • The booktab environment seems more proper for my table, like the explanation for the left figure. Your answer is so good!!!
    – Windy
    Commented Jan 3, 2015 at 22:56
  • @Windy Thanks. When you are ready, please consider accepting an answer (mine or someone else's) by clicking on the greyed-out checkmark at the top left of the relevant answer. This helps others see that your question is answered. Disclaimer: it gains me points. Bonus: you get points, too!
    – cfr
    Commented Jan 3, 2015 at 23:03
5

You probably need an adjustbox environment with top alignment for your tabular.

So, load the adjustbox package and embed your tabular in

\begin{adjustbox}{valign=t}
....
\end{adjustbox}

MWE:

\documentclass{article}
\usepackage{tabularx}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{lipsum} % just for the example
\usepackage{adjustbox}


\begin{document}
\begin{table}
\centering
\begin{tabularx}{\textwidth}{ |l|X| }
\hline
 \multicolumn{1}{c|}{%
 \begin{adjustbox}{valign=t}
 \begin{tabular}[c]{@{}c@{}}
  \begin{tikzpicture}
    \draw[red,pattern=north east lines, pattern color=red] (0,0.1) rectangle (1,0.4);
    \draw [blue,very thick](0,0) rectangle (4,0.5);
    \draw[thick,<->] (0,-0.2) -- (4,-0.2);
    \node at (2,-0.5) {$w/2$};
    \draw [blue,very thick](1,1.5) arc [radius=1, start angle=0, delta angle=-90];
    \draw[thick,<->] (0,1.5) -- (1,1.3);
    \node at (0.5,1.8) {$R$};
    \draw[very thick,->, red] (0,1.5) -- (0,0.9);
    \node[red] at (-0.2,1.2) {$\delta$};
    \draw[thick,<->] (4.2,0) -- (4.2,0.5);
    \node at (4.5,0.25) {$h$};
    \draw[thick,dashed,->](0,0) -- (4.7,0) node[anchor=north west] {x};
    \draw[thick,dashed,->] (0,0) -- (0,2.2) node[anchor=south east] {y};
   \end{tikzpicture} \\$h=1\;\;R=4.366h\;\;w/2=20h$
 \end{tabular}
 \end{adjustbox}}
& \lipsum[2]\\\hline
   \end{tabularx}
   \end{table}
\end{document} 

Output:

enter image description here

2
  • Your solution is quite convenient to adjust the figure!!!:)
    – Windy
    Commented Jan 3, 2015 at 22:57
  • @Windy You're welcome. Remember that the best way to say thanks here is to accept (and upvote) answers that solve your problems. See How do you accept an answer? for more info. Commented Mar 13, 2015 at 16:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .