Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to add a box in a tabularx table that would frame several cells vertically. There is this post that shows how to frame cells horizontally (Framing cells in a table), but I cannot figure out how to do to have the box extend over several rows. Here is what I would roughly like to get (but with a nice box):

Column 1 & Column 2 & Column 3
                      +-----+
bla      & bla      & |bla  |
                      |     |
bla      & bla      & |bla  |
                      +-----+

Any idea of how I could do that?

Edit: the solutions proposed below are very nice. But they are compatible with the tabular environment only, it seems, and not with tabularx and booktabs. Below is the code I have used. Does anyone have an idea of how to extend these solutions to the tabularx style? If not, then I will just rewrite all my tables using the simple tabular style.

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

   \newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
   \newcommand{\DrawBox}[1][]{%
        \tikz[overlay,remember picture]{
        \draw[black,#1]
           ($(left)+(-0.5em,2.0ex)$) rectangle
           ($(right)+(0.75em,-0.75ex)$);}
          }

   \begin{document}
   \begin{table}[H]
   \centering
    \begin{tabularx}{0.3\textwidth}{Y*{4}{c}}
        \toprule
         & Masc  & Fem & Neut\\ 
        \midrule  \cline{4-4}
        Nom & hic & haec & \multicolumn{1}{|c|}{hoc} \\ 
        Acc & hunc & hanc & \multicolumn{1}{|c|}{hoc} \\ \cline{4-4}
        \bottomrule 
    \end{tabularx} 
   \end{table}
     \begin{table}[H]
   \centering
    \begin{tabularx}{0.3\textwidth}{Y*{4}{c}}
        \toprule
         & Masc  & Fem & Neut\\ 
        \midrule  
        Nom & hic & haec & \tikzmark{left}hoc \\ 
        Acc & hunc & hanc & hoc\tikzmark{right} \\ 
        \bottomrule 
    \end{tabularx} 
     \end{table}
    \end{document}
share|improve this question
2  
Please always provide a complete test document, but you can do the horizontal parts with \cline and the vertical parts with | –  David Carlisle Mar 26 at 22:07
    
Both solutions are compatible with tabularx. Show a complete MWE that is not working. –  Heiko Oberdiek Mar 27 at 3:55
    
You are noy using \DrawBox in your second code, so the box is not drawn. Add \DrawBox just before \end{table}. –  Gonzalo Medina Mar 27 at 13:58
    
Have updated solution which provides for a more flexible \DrawBox macro (using booktabs), along with some usage notes. This will work just a well with tabularx. –  Peter Grill Mar 28 at 19:51

2 Answers 2

up vote 10 down vote accepted
\documentclass{article}

\begin{document}
  \begin{tabular}{*6{c}}\hline
    Col 1 & Col 2 & Col 3 & Col 4 & Col 5 & Col 6 \\\cline{1-1}
    \multicolumn{1}{|c|}{bla}   & bla   & bla   & bla   & bla   & bla \\
    \multicolumn{1}{|c|}{bla}   & bla   & bla   & bla   & bla   & bla \\ 
    \multicolumn{1}{|c|}{ble} & ble & ble & bla & bla & bla \\ \cline{1-1}
    bla   & bla   & bla   & bla   & bla   & bla \\ \hline
  \end{tabular}
\end{document}

enter image description here

With tabularx and booktabs:

   \documentclass{article}
   \usepackage{tabularx, booktabs}
   \begin{document}
   \begin{table}[H]
   \centering
    \begin{tabularx}{0.4\textwidth}{X*{4}{c}}      %% 0.3\textwidth too narrow 
                                                   %% and Y column type not known
        \toprule
         & Masc  & Fem & Neut\\
        \midrule  \cline{4-4}
        Nom & hic & haec & \multicolumn{1}{|c|}{hoc} \\
        Acc & hunc & hanc & \multicolumn{1}{|c|}{hoc} \\ \cline{4-4}
        \bottomrule
    \end{tabularx}
   \end{table}
    \end{document}

enter image description here

share|improve this answer

There is always the tikz solution. Here is one that use bascially the same code from Highlight elements in the matrix but with a more flexible \DrawBox macro. And since it is tikz you get all the additional flexibility that comes with that:

enter image description here

Usage:

  1. You use \tikzmark{<name>} to "mark" a particular location with <name>.
  2. These named locations are passed to the \DrawMacro which uses these are the boundary of the drawing. You need one \DrawBox call for each box that you wish to draw.

Note:

Code:

\documentclass{article}
\usepackage{tikz}
\usepackage{booktabs}
\usetikzlibrary{calc}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[3][]{%
    \tikz[overlay,remember picture]{
    \draw[black,#1]
      ($(#2)+(-0.5em,2.0ex)$) rectangle
      ($(#3)+(0.75em,-0.75ex)$);}
}

\begin{document}
\begin{tabular}{*{7}{c}}\toprule
    Col 1 &  Col 2                   &       Col  3                 & Col 4                   & Col 5 & Col 6                         & Col 7 \\
    \cmidrule(lr){2-6}
     0    &   1                      & \tikzmark{top left 1}abcd    &   0                     &   0   &   0                           &   1   \\
     1    &   \tikzmark{top left 3}0 & www                          &   0                     &   0   &   0                           &   1   \\
     0    &   1                      & xyz\tikzmark{bottom right 1} &  \tikzmark{top left 2}1 &   0   &   0                           &   0   \\
     0    &   0                      & 1                            &   0                     &   0   &   0\tikzmark{bottom right 3}  &   0   \\
     0    &   0                      & 0                            &   0                     &   0   &   1                           &   0\tikzmark{bottom right 2} \\
     0    &   0                      & 1                            &   0                     &   0   &   0                           &   0 \\
     \bottomrule
  \end{tabular}

\DrawBox[ultra thick, red]{top left 1}{bottom right 1}
\DrawBox[ultra thick, blue]{top left 2}{bottom right 2}
\DrawBox[ultra thick, draw=green, dotted, fill=yellow!15, fill opacity=0.3]{top left 3}{bottom right 3}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.