在表格内创建框架

在表格内创建框架

我想在我的表格中放置一个框架/框,例如,我有一张表格 ala

\begin{tabular}{ |c|l| *{3}{>{\centering\arraybackslash}p{2em}} |c|c|c| }
    \hline
    Problem $I_k$ & Solved $C_k$ & \multicolumn{3}{ c |}{Solution to $C_k$} & Current & Pursue & Stored\\
    \cline{3-5}
    & & $x_1$ & $x_2$ & $z$ & & & \\ \hline
    $I_0$ & $C_0$ & $\frac{44}{9}$ & $\frac{86}{9}$ & $\frac{188}{3}$ & - & $I_0$ & - \\ \hline
    $I_1$ & $C_1$ & $4$ & $10$ & $60$ & 60 & $I_0$ & - \\
    $I_2$ & $C_2$ & $5$ & $\frac{47}{5}$ & $\frac{313}{5}$ & & & \\ \hline
\end{tabular}

渲染结果如下

在此处输入图片描述

现在,我想要的是类似以下内容

在此处输入图片描述

这可能吗?如果可以,我该怎么做?

答案1

使用tikzmark:P

在此处输入图片描述

\documentclass{article}
\usepackage{tabularx}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}

\begin{document}
\setlength{\extrarowheight}{1ex}
\begin{tabular}{ |c|l| *{3}{>{\centering\arraybackslash}p{2em}} |c|c|c| }
    \hline
    Problem $I_k$ & Solved $C_k$ & \multicolumn{3}{ c |}{Solution to $C_k$} & Current & Pursue & Stored\\
    \cline{3-5}
    & & $x_1$ & $x_2$ & $z$ & & & \\ \hline
    $I_0$ & $C_0$ & $\frac{44}{9}$ & $\frac{86}{9}$ & $\frac{188}{3}$ & - & $I_0$ & - \\ \hline
    $I_1$ & $C_1$ & \tikzmark{a}$4$ & $10$ & $60$\tikzmark{b} & 60 & $I_0$ & - \\
    $I_2$ & $C_2$ & $5$ & $\frac{47}{5}$ & $\frac{313}{5}$ & & & \\ \hline
\end{tabular}
\begin{tikzpicture}[remember picture,overlay]
  \draw ($(pic cs:a)+(-\fboxsep,-\fboxsep)$) rectangle ($(pic cs:b)+(\fboxsep,1em)$);
\end{tikzpicture}
\end{document}

答案2

由于已知框列的宽度(每个 2em),因此可以计算出框尺寸:

\documentclass{article}
\usepackage{array}
\begin{document}
\renewcommand*{\arraystretch}{1.5}
\begin{tabular}{ |c|l| *{3}{>{\centering\arraybackslash}p{2em}} |c|c|c| }
    \hline
    Problem $I_k$ & Solved $C_k$ & \multicolumn{3}{ c |}{Solution to $C_k$} & Current & Pursue & Stored\\
    \cline{3-5}
    & & $x_1$ & $x_2$ & $z$ & & & \\ \hline
    $I_0$ & $C_0$ & $\frac{44}{9}$ & $\frac{86}{9}$ & $\frac{188}{3}$ & - & $I_0$ & - \\ \hline
    $I_1$ & $C_1$ &
      \multicolumn{3}{l|}{%
        \setlength{\fboxrule}{.8pt}%
        \sbox0{$4$}%
        \kern\dimexpr 1em-\fboxrule-\fboxsep-.5\wd0\relax
        \fbox{%
          \copy0\relax
          \kern\dimexpr -.5\wd0 + 1em + 2\tabcolsep\relax
          \hbox to 2em{\hfill$10$\hfill}%
          \sbox0{$60$}%
          \kern\dimexpr 2\tabcolsep + 1em - .5\wd0\relax
          \box0\relax
        }%
      }%
      & 60 & $I_0$ & - \\
    $I_2$ & $C_2$ & $5$ & $\frac{47}{5}$ & $\frac{313}{5}$ & & & \\ \hline
\end{tabular}
\end{document}

结果

相关内容