表格单元格周围的矩形

表格单元格周围的矩形

我问了一个问题这里但是,相当愚蠢的是,在回答之后又添加了额外的信息。

我的附加问题是:我有一张表格,里面的单词长度各不相同。我想包围不同的单元格组,包括对角线。如果可能的话,如果矩形尽可能不相互重叠就更好了。

我看了答案这里,虽然很乏味,但除了重叠之外还是有效的。

示例图像

这是我目前所取得的进展:

\documentclass[fleqn,12pt,a4paper,landscape]{article}

\usepackage{tikz}
\usepackage{tabularx}

\begin{document}

% Introduce a new counter for counting the nodes needed for circling
\newcounter{nodecount}
% Command for making a new node and naming it according to the nodecount     counter
\newcommand\tabnode[1]{\addtocounter{nodecount}{1} \tikz \node     (\arabic{nodecount}) {#1};}

% Some options common to all the nodes and paths
\tikzstyle{every picture}+=[remember picture,baseline]
\tikzstyle{every node}+=[inner sep=0pt,anchor=base,
minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
\tikzstyle{every path}+=[thick, rounded corners]

\begin{table}[ht]
  \begin{minipage}[b]{1 \linewidth}\centering
   \begin{tabular}{cccccc}
% Turn the cells needed for defining the circling paths into nodes with our     custom command
\tabnode{Long ago}& \tabnode{A few days ago}& \tabnode{Day before         yesterday}& \tabnode{Yesterday} &  &  \\
     & & &  & \tabnode{Today} & \tabnode{Now}\\
     \tabnode{Long time in the future}& \tabnode{A few days on}&     \tabnode{Day after tomorrow}& \tabnode{Tomorrow} &  & \\
     \end{tabular}
   \end{minipage}


\begin{tikzpicture}[overlay]
% Define the circle paths
\draw [blue](1.north west) -- (1.north east) -| (7.north east) |- (7.south     west) |- (1.north west);
\draw [red](8.south west) -| (2.north east) -| (2.south west) |- (8.south     west);
\draw [yellow](9.south west) -| (3.north east) -| (3.south west) |- (9.south     west);
\draw [green](8.south west) -| (3.north east) -| (2.south west) |- (8.south     west);


\end{tikzpicture}

\end{table}
\end{document} 
%%% Local Variables: 
%%% mode: latex
%%% TeX-master: t
%%% End: 

现在的进展

答案1

fit有了图书馆,这很容易。

\documentclass[]{article}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,fit}
\begin{document}
\begin{tikzpicture}%[thick]
  \matrix (M) [%
    matrix of nodes, column sep=1cm, row sep=1cm
  ]
  {%
     A1& B1& C1& D1 & E1 & F1 \\
     A2& B2& C2& D2 & E2 & F2 \\
     A3& B3& C3& D3 & E3 & F3 \\
     A4& B4& C4& D4 & E4 & F4 \\
  };
\node[draw=blue,rounded corners = 1ex,fit=(M-1-1)(M-2-1),inner sep = 0pt] {};
\node[draw=red,rounded corners = 1ex,fit=(M-1-2)(M-2-2),inner sep = 0pt] {}; 
\node[draw=green,rounded corners = 1ex,fit=(M-1-3)(M-2-3),inner sep = 0pt] {};
\node[draw=purple!50,rounded corners = 1ex,fit=(M-1-4)(M-2-4),inner sep = 0pt] {};
\node[draw=cyan,rounded corners = 1ex,fit=(M-1-5)(M-2-5),inner sep = 0pt] {};
\node[draw=magenta,rounded corners = 1ex,fit=(M-2-2)(M-1-3),inner sep = 1pt] {};
\node[draw=brown,rounded corners = 1ex,fit=(M-2-3)(M-1-4),inner sep = 2pt] {};
\node[draw=red!40!white,rounded corners = 1ex,fit=(M-2-1)(M-1-3),inner sep = 3pt] {};
\node[draw=yellow,rounded corners = 1ex,fit=(M-2-1)(M-1-4),inner sep = 4pt] {};
\end{tikzpicture}
\end{document}

在此处输入图片描述

调整node sep以满足您的需求。您还outer sep可以使用 进行尝试。

完整图像将是:

在此处输入图片描述

您使用fit库编写的代码将是:

\documentclass[fleqn,12pt,a4paper,landscape]{article}

\usepackage{tikz}
\usepackage{tabularx}
\usetikzlibrary{fit}

\begin{document}

% Introduce a new counter for counting the nodes needed for circling
\newcounter{nodecount}
% Command for making a new node and naming it according to the nodecount     counter
\newcommand\tabnode[1]{\addtocounter{nodecount}{1} \tikz \node  (\arabic{nodecount}) {#1};}

% Some options common to all the nodes and paths
\tikzstyle{every picture}+=[remember picture,baseline]
\tikzstyle{every node}+=[anchor=base,
minimum width=1.8cm,align=center,text depth=.25ex,outer sep=1.5pt]
\tikzstyle{every path}+=[thick, rounded corners]

\begin{table}[ht]
  \begin{minipage}[b]{1 \linewidth}\centering
   \begin{tabular}{cccccc}
% Turn the cells needed for defining the circling paths into nodes with our     custom command
\tabnode{Long ago}& \tabnode{A few days ago}& \tabnode{Day before         yesterday}& \tabnode{Yesterday} &  &  \\
     & & &  & \tabnode{Today} & \tabnode{Now}\\
     \tabnode{Long time in the future}& \tabnode{A few days on}&     \tabnode{Day after tomorrow}& \tabnode{Tomorrow} &  & \\
     \end{tabular}
   \end{minipage}


\begin{tikzpicture}[overlay]
% Define the circle paths
\node[draw=blue,rounded corners = 1ex,fit=(1)(7),inner sep = 0pt] {};
\node[draw=red,rounded corners = 1ex,fit=(2)(8),inner sep = 0pt] {};
\node[draw=yellow,rounded corners = 1ex,fit=(3)(9),inner sep = 0pt] {};
\node[draw=green,rounded corners = 1ex,fit=(8)(3),inner sep = 4pt] {};
\end{tikzpicture}

\end{table}
\end{document}

在此处输入图片描述

答案2

这是基于您第一张海报的方法的另一种选择。这可以作为起点。 在此处输入图片描述

\documentclass[]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{matrix,shapes,arrows,positioning}

\begin{document}
\begin{tikzpicture}

  \matrix (mat) [matrix of nodes, column sep=0.8cm, row sep=0.1cm] 
  {%
     Long ago                      & A few days ago            &Day before yesterday & Yesterday &     \phantom{Today  now}               \\ 
                                         &                                    &                                 &                 & Today  now  \\
     Long time in the future  & Day after tomorrow      & Days after tomorrow & Tomorrow &    \phantom{Today  now}       \\ \\
  };  
\draw[blue,very thick, rounded corners] (mat-1-1.north west) -- (mat-1-1.north east) -- ++ (1.1cm,0) -- (mat-3-1.south east) -- (mat-3-1.south west) |- (mat-1-1.north west); 
\draw[red,very thick, rounded corners] (mat-1-2.north west) -- (mat-1-2.north east) -- ++ (0.35cm,0) -- (mat-3-2.south east) -- (mat-3-2.south west) |- (mat-1-2.north west); 
\draw[purple,very thick, rounded corners] (mat-1-4.north west) -| (mat-3-4.south east) -- (mat-3-4.south west) -- cycle;

\draw[blue,very thick, rounded corners] (mat-1-5.north west) -- (mat-1-5.north east)   -- (mat-3-5.south east) -- (mat-3-5.south west) -- cycle; 

%%%%%% Here is how (the yellow outer border ) where I define 4 corners around the matrix nodes. Same idea applies to other outer borders. %%%%

\node [coordinate, shift={(-1.5cm,0.5cm)},inner sep=0pt] at (mat-1-1.north west)  (c1){};
\node [coordinate,shift={(0.5cm,0.5cm)},inner sep=0pt] at (mat-1-4.north east) (c2){};
\node [coordinate,shift={(0.5cm,-0.5cm)},inner sep=0pt] at (mat-3-4.south east) (c3){};
\node [coordinate,shift={(-0.4cm,-0.5cm)},inner sep=0pt] at (mat-3-1.south west) (c4){};
\draw [yellow,very thick, rounded corners] (c1) -- (c2) -- (c3) -- (c4) -- cycle; 
\end{tikzpicture}
\end{document}

相关内容