如何将 2 个表格 + 图表并排放置?

如何将 2 个表格 + 图表并排放置?

我是 Tex 的新手,我有一个问题。

这是我的代码:

\begin{table}[!htb]


\textbf{Gruppe 1:} \\\\
\begin{tabular}{l| r} 
Student 1 & Student 2 \\\hline
$s1$ & $s2$ \\ 
$s3$ & $s4$
\end{tabular} 
\\\\\\
\begin{tikzpicture}[main_node/.style={circle,fill=blue!20,draw,minimum size=1em,inner sep=3pt]}]

\node[main_node] (1) at (-1,0) {s1};
\node[main_node] (2) at (-1, -1.5)  {s2};
\node[main_node] (3) at (1, 0) {s3};
\node[main_node] (4) at (1, -1.5) {s4};

\draw (1) -- (2) (3) -- (4);
\\\\
\end{tikzpicture} \\\\

\textbf{Gruppe 2:} \\\\
\begin{tabular}{l|r}

Student 1 & Student 2 \\\hline
$s1$ & $s3$ \\
$s2$ & $s4$
\end{tabular} 
\\\\

\begin{tikzpicture}[main_node/.style={circle,fill=blue!20,draw,minimum 
size=1em,inner sep=3pt]}][h!]

\node[main_node] (1) at (-1,0) {s1};
\node[main_node] (2) at (-1, -1.5)  {s2};
\node[main_node] (3) at (1, 0) {s3};
\node[main_node] (4) at (1, -1.5) {s4};

\draw (1) -- (3)  (2) -- (4);

\end{tikzpicture}\\\\


\textbf{Gruppe 3:} \\\\
\begin{tabular}{l|r} 
Student 1 & Student 2 \\\hline
$s1$ & $s4$ \\
$s3$ & $s2$
\end{tabular}\\\\\\
\begin{tikzpicture}[main_node/.style={circle,fill=blue!20,draw,minimum 
size=1em,inner sep=3pt]}]

\node[main_node] (1) at (-1,0) {s1};
\node[main_node] (2) at (-1, -1.5)  {s2};
\node[main_node] (3) at (1, 0) {s3};
\node[main_node] (4) at (1, -1.5) {s4};

\draw (1) -- (4) (3) -- (2);

\end{tikzpicture}

\end{table}

我的问题是:Exercise 1.1.a)我想让Gruppe 1整个区块紧挨着Gruppe 2整个区块。该怎么做?

答案1

像这样?

在此处输入图片描述

您的问题不清楚,所以我猜上面的图片展示了您想要实现的目标:

  • 在文本中你说的是两个表,但在代码中却是三个
  • 从代码片段可以得出结论,图像位于表格下方

姆韦(最小工作示例)生成上述图像的方法是:

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{tabularx}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage{amsmath}

\begin{document}
\tikzset{
every node/.style = {circle,fill=blue!20,draw,minimum size=1em,inner sep=3pt},
    node distance = 8mm and 12mm
        }
\noindent
\begin{tabularx}{\linewidth}{CCC}
    \centering
    \textbf{Gruppe 1:}
\[
\begin{array}{l | r}
\text{Student 1}    &   \text{Student 2}    \\
    \hline
                s1  &   s2                  \\
                s3  &   s4
\end{array}
\]
\begin{tikzpicture}
\node (n1)                  {s1};
\node (n2) [below=of n1]    {s2};
\node (n3) [right=of n1]    {s3};
\node (n4) [below=of n3]    {s4};
%
\draw (n1) -- (n2) (n3) -- (n4);
\end{tikzpicture}
    &
    \textbf{Gruppe 2:}
\[
\begin{array}{l | r}
\text{Student 1}    &   \text{Student 2}    \\
    \hline
                s1  &   s2                  \\
                s3  &   s4
\end{array}
\]
\begin{tikzpicture}
\node (n1)                  {s1};
\node (n2) [below=of n1]    {s2};
\node (n3) [right=of n1]    {s3};
\node (n4) [below=of n3]    {s4};
%
\draw (n1) -- (n3) (n2) -- (n4);
\end{tikzpicture}
    &
    \textbf{Gruppe 3:}
\[
\begin{array}{l | r}
\text{Student 1}    &   \text{Student 2}    \\
    \hline
                s1  &   s2                  \\
                s3  &   s4
 \end{array}
\]
\begin{tikzpicture}
\node (n1)                  {s1};
\node (n2) [below=of n1]    {s2};
\node (n3) [right=of n1]    {s3};
\node (n4) [below=of n3]    {s4};
%
\draw (n1) -- (n4) (n2) -- (n3);
\end{tikzpicture}
\end{tabularx}
\end{document} 

答案2

如果我理解正确的话,您只想有两个组并排。

由于您的图像(不包括线条)相同,因此我还创建了一个pic使用tikz matrix来绘制它们的 。这避免了代码行的重复。

\documentclass[a4paper,11pt]{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\tikzset{
    main_node/.style = {circle,fill=blue!20,draw,minimum size=1em,inner sep=3pt},
    pics/mymatrix/.style={code={%
        \matrix[matrix of nodes,
            ampersand replacement=\&,
            nodes={main_node}, 
            column sep= 12mm,
            row sep=8mm] {%
                |[name=1]| {s1} \& |[name=3]| {s3}\\
                |[name=2]| {s2} \& |[name=4]| {s4}\\
        };
    }},
}

\usepackage{array}
\renewcommand{\arraystretch}{1.2}

\begin{document}
\begin{table}[!htb]
    \centering
    \begin{tabular}{c@{\hspace{3em}}c}% <-- \hspace{3em} is the additional space between the two groups
    \textbf{Gruppe 1:} &
    \textbf{Gruppe 2:} \\
    \begin{tabular}{l| r} 
        Student 1 & Student 2 \\\hline
        $s1$ & $s2$ \\ 
        $s3$ & $s4$
    \end{tabular} 
    &
    \begin{tabular}{l|r}
        Student 1 & Student 2 \\\hline
        $s1$ & $s3$ \\
        $s2$ & $s4$
    \end{tabular} 
    \\[5ex]% <-- additional space between the tables and the picture
    \begin{tikzpicture}
        \pic {mymatrix};
        \draw (1) -- (2) (3) -- (4);
    \end{tikzpicture} 
    &
    \begin{tikzpicture}
        \pic {mymatrix};
        \draw (1) -- (3)  (2) -- (4);
    \end{tikzpicture}
    \\
    \end{tabular}
    \vspace{4ex}% <-- additional space between the 1-2 groups and the 3

    \begin{tabular}{c}
    \textbf{Gruppe 3:}  \\
    \begin{tabular}{l|r} 
        Student 1 & Student 2 \\\hline
        $s1$ & $s4$ \\
        $s3$ & $s2$
    \end{tabular}
    \\[5ex]% <-- additional space between the tables and the picture
    \begin{tikzpicture}
        \pic {mymatrix};
        \draw (1) -- (4) (3) -- (2);
    \end{tikzpicture}
    \\
    \end{tabular}
\end{table}
\end{document}

在此处输入图片描述

相关内容