将表格和图表对齐在同一行

将表格和图表对齐在同一行

我希望将表格和图表垂直对齐在一条线上。这是我的代码:

\begin{flushleft}
\begin{tabular}{l||c|r}
              & a & b \\
\hline
$q_0           & q_1 & q_0$ \\
$q_1           & q_1 & q_2$ \\
$q_2           & q_2 & q_2$
\end{tabular}
\end{flushleft}
\begin{flushright}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
   \node[state,initial,accepting] (q_0)   {$q_0$};
   \node[state,accepting] (q_1) [above right=of q_0] {$q_1$};
   \node[state] (q_2) [below right=of q_1] {$q_2$};
    \path[->]
    (q_0) edge  node {a} (q_1)
          edge  [loop above] node {b} ()
    (q_1) edge  node  {b} (q_2)
          edge [loop above] node {a} ()
    (q_2) edge [loop below] node {a,b} ();
\end{tikzpicture}
\end{flushright}

目前,我的输出如下:

在此处输入图片描述

  1. 如何使表格和图表垂直对齐?

感谢大家的帮助

答案1

另一种方法是使用minipage环境分别option [t,c,b]{width of choice}进行顶部、中心和底部对齐。

在此处输入图片描述

代码

\documentclass[a4paper,11pt]{article}
\usepackage[margin=10pt]{geometry}
\usepackage{tikz}

\usetikzlibrary{positioning,automata}


\begin{document}

\begin{minipage}[c]{0.3\textwidth}
\vspace{0pt}
\begin{tabular}{l||c|r}
              & a & b \\\hline
$q_0$           &$ q_1$ & $q_0$ \\
$q_1$           & $q_1$ & $q_2$ \\
$q_2$           & $q_2$ & $q_2$
\end{tabular}
\vspace{0pt}
\end{minipage}
\noindent
\begin{minipage}[c]{0.3\textwidth}
\vsapce{0pt}
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
   \node[state,initial,accepting] (q_0)   {$q_0$};
   \node[state,accepting] (q_1) [above right=of q_0] {$q_1$};
   \node[state] (q_2) [below right=of q_1] {$q_2$};
    \path[->]
    (q_0) edge  node {a} (q_1)
          edge  [loop above] node {b} ()
    (q_1) edge  node  {b} (q_2)
          edge [loop above] node {a} ()
    (q_2) edge [loop below] node {a,b} ();
\end{tikzpicture}
\end{minipage}


\end{document}

tabular带有包的环境也是array一个可能的解决方案

在此处输入图片描述

代码

\documentclass[a4paper,11pt]{article}
\usepackage[margin=10pt]{geometry}
\usepackage{tikz,array}                % require array package here
\usetikzlibrary{positioning,automata}

\begin{document}
\hrulefill

\begin{tabular}{>{\centering\arraybackslash}m{5cm}>{\centering\arraybackslash}m{5cm}}
\begin{tabular}{l||c|r}
                & a & b \\\hline
$q_0$           &$ q_1$ & $q_0$ \\
$q_1$           & $q_1$ & $q_2$ \\
$q_2$           & $q_2$ & $q_2$
\end{tabular}   &
\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
   \node[state,initial,accepting] (q_0)   {$q_0$};
   \node[state,accepting] (q_1) [above right=of q_0] {$q_1$};
   \node[state] (q_2) [below right=of q_1] {$q_2$};
    \path[->]
    (q_0) edge  node {a} (q_1)
          edge  [loop above] node {b} ()
    (q_1) edge  node  {b} (q_2)
          edge [loop above] node {a} ()
    (q_2) edge [loop below] node {a,b} ();
\end{tikzpicture}
\end{tabular}

\hrulefill

\end{document}

答案2

以下是基于 \savebox 和 \raisebox 的通用解决方案。

\documentclass{article}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{positioning,automata}

\newsavebox{\boxA}
\newsavebox{\boxB}
\newlength{\lenA}
\newlength{\lenB}

\begin{document}

\savebox{\boxA}
{\begin{tabular}[c]{l||c|r}
              & a & b \\
\hline
$q_0$           & $q_1$ & $q_0$ \\
$q_1$           & $q_1$ & $q_2$ \\
$q_2$           & $q_2$ & $q_2$
\end{tabular}}
\settoheight{\lenA}{\usebox{\boxA}}

\savebox{\boxB}
{\begin{tikzpicture}[shorten >=1pt,node distance=2cm,on grid,auto]
   \node[state,initial,accepting] (q_0)   {$q_0$};
   \node[state,accepting] (q_1) [above right=of q_0] {$q_1$};
   \node[state] (q_2) [below right=of q_1] {$q_2$};
    \path[->]
    (q_0) edge  node {a} (q_1)
          edge  [loop above] node {b} ()
    (q_1) edge  node  {b} (q_2)
          edge [loop above] node {a} ()
    (q_2) edge [loop below] node {a,b} ();
\end{tikzpicture}}
\settoheight{\lenB}{\usebox{\boxB}}

\noindent
Align tops:

\noindent
\hfil\raisebox{-\lenA}{\usebox{\boxA}}
\hfil\raisebox{-\lenB}{\usebox{\boxB}}

\noindent
Align centers:

\noindent
\hfil\usebox{\boxA}
\hfil\raisebox{-0.5\lenB}{\usebox{\boxB}}

\noindent
Align bottoms:

\noindent
\hfil\raisebox{\lenA}{\usebox{\boxA}}
\hfil\usebox{\boxB}

\end{document}

例子

由于某些奇怪的原因,\lenA 的大小只有预期的一半。我猜 \settoheight 测量的是顶部到基线的距离,而不是底部到基线的距离。

相关内容