4

I know that there is a way to cross out a row of a Tabular, by using \hline. Is there an easy to cross out a column of a Tabular?enter image description here

The picture below is a Tabular, where some of the rows are crossed out. I would love to cross out column.

  • 1
    Welcome to TeX.SX! Could you please show us what you want to achieve (best by picture) and what you've tried so far (code example)? – TeXnician Apr 12 '18 at 05:14
  • @TeXnician I hope the picture helps. The method for crossing out row is introduced in https://tex.stackexchange.com/questions/350150/strike-out-an-entire-line-in-table?noredirect=1&lq=1 – Jialin Zhang Apr 12 '18 at 05:23
  • I meant more what you would like to achieve. As I've understood your post you wanted to cross out a column and not a row. But there are many ways to do so (one line, some kind of X, …) – TeXnician Apr 12 '18 at 05:24
  • I would love to cross out column $m_3=50$. – Jialin Zhang Apr 12 '18 at 05:26

4 Answers4

2

I once used something like the following code. Because perpendicular lines don't work well in this example I have added different styles to cross out columns.

It can also cross out rows but there I have not implemented different styles.

table-strikethrough.tex:

% ========== settings ==========
\newcommand{\strikethroughcolor}{black!50}
\newcommand{\strikethroughwidth}{.25pt}

\newcommand{\strikethroughCrossWidth}{3em}


% ========== styles ==========
\makeatletter
\newcommand{\strikethroughColumnStylePerpendicular}{\let\@markcol@start=\@markcol@start@perpendicular}
\newcommand{\strikethroughColumnStyleDiagonal}{\let\@markcol@start=\@markcol@start@diagonal}
\newcommand{\strikethroughColumnStyleCounterdiagonal}{\let\@markcol@start=\@markcol@start@counterdiagonal}
\newcommand{\strikethroughColumnStyleCross}{\let\@markcol@start=\@markcol@start@cross}


% ========== preparation ==========
\usepackage{array}
\newcounter{tabularidcounter}
\newif\if@firstcol
% -----  rows -----
\newcounter{tabularlinecounter}
\newcommand*{\setLinesToCross}[1]{%
    \gdef\@lines{#1}%
    \setcounter{tabularlinecounter}{-1}%
    \stepcounter{tabularidcounter}%
}
\newcolumntype{A}{>{%
    \setcounter{tabularcolcounter}{0}%
    \typeout{reset col counter: \thetabularcolcounter}%
    \stepcounter{tabularlinecounter}%
    \@expandtwoargs\in@{,\thetabularlinecounter,}{,\@lines,}%
    \ifin@
        \@marklines@start
    \fi
    \@firstcoltrue
}}
\newcolumntype{Z}{<{%
    \@expandtwoargs\in@{,\thetabularlinecounter,}{,\@lines,}%
    \ifin@
        \@marklines@end
    \fi
}}

% -----  cols -----
\newcounter{tabularcolcounter}
\newcommand*{\setColsToCross}[1]{%
    \gdef\@cols{#1}%
    \stepcounter{tabularidcounter}%
}
\newcommand{\coltop}{%
    \stepcounter{tabularcolcounter}%
    \if@firstcol
        \@marklines@savexleft
    \fi
    \hfill
    \@expandtwoargs\in@{,\thetabularcolcounter,}{,\@cols,}%
    \ifin@
        \@markcol@start
    \fi
    \hfill
    \@marklines@savexright
}
\newcommand{\colbottom}{%
    \stepcounter{tabularcolcounter}%
    \typeout{step col counter: \thetabularcolcounter}%
    \@expandtwoargs\in@{,\thetabularcolcounter,}{,\@cols,}%
    \ifin@
        \@markcol@end
    \fi
}


% ========== implementation ==========
\usepackage{tikz}
\usetikzlibrary{tikzmark}
% -----  rows -----
\newcommand*\@marklines@savexleft{%
    \pgfmark{tab\thetabularidcounter-rowstart-x}%
}
\newcommand*\@marklines@savexright{%
    \pgfmark{tab\thetabularidcounter-rowend-x}%
}
\newcommand*\@marklines@start{%
    \tikz[remember picture,overlay]
        \draw[yshift=.65ex, draw=\strikethroughcolor, line width=\strikethroughwidth] ({pic cs:tab\thetabularidcounter-rowstart-x} |- 0, 0) -- 
            ({pic cs:tab\thetabularidcounter-rowend-\thetabularlinecounter} -| {pic cs:tab\thetabularidcounter-rowend-x});%
}
\newcommand*\@marklines@end{%
    \pgfmark{tab\thetabularidcounter-rowend-\thetabularlinecounter}%
}

% -----  cols -----
\newcommand*\@markcol@start@perpendicular{%
    \tikz[remember picture,overlay]
        \draw[draw=\strikethroughcolor, line width=\strikethroughwidth] (0, .6em) -- ([yshift=-.2em] pic cs:tab\thetabularidcounter-colend-\thetabularcolcounter);%
}
\newcommand*\@markcol@start@diagonal{%
    \tikz[remember picture,overlay]
        \draw[draw=\strikethroughcolor, line width=\strikethroughwidth] (-\strikethroughCrossWidth/2, .6em) -- ([yshift=-.2em, xshift=\strikethroughCrossWidth/2] pic cs:tab\thetabularidcounter-colend-\thetabularcolcounter);%
}
\newcommand*\@markcol@start@counterdiagonal{%
    \tikz[remember picture,overlay]
        \draw[draw=\strikethroughcolor, line width=\strikethroughwidth] (\strikethroughCrossWidth/2, .6em) -- ([yshift=-.2em, xshift=-\strikethroughCrossWidth/2] pic cs:tab\thetabularidcounter-colend-\thetabularcolcounter);%
}
\newcommand*\@markcol@start@cross{%
    \begin{tikzpicture}[remember picture,overlay]
        \draw[draw=\strikethroughcolor, line width=\strikethroughwidth] (-\strikethroughCrossWidth/2, .6em) -- ([yshift=-.2em, xshift=\strikethroughCrossWidth/2] pic cs:tab\thetabularidcounter-colend-\thetabularcolcounter);%
        \draw[draw=\strikethroughcolor, line width=\strikethroughwidth] (\strikethroughCrossWidth/2, .6em) -- ([yshift=-.2em, xshift=-\strikethroughCrossWidth/2] pic cs:tab\thetabularidcounter-colend-\thetabularcolcounter);%
    \end{tikzpicture}%
}
\newcommand*\@markcol@end{%
    \pgfmark{tab\thetabularidcounter-colend-\thetabularcolcounter}%
}


% ========== first row & last row ==========
\usepackage{booktabs}
\usepackage{multido}
\usepackage{etoolbox}

\newcommand{\stTableStart}[1]{%
    \def\tmp@tmprow{}%
    % I can't use pgffor's \foreach here because there each iteration is executed in a different group.
    \Multido{}{\number\numexpr #1-1\relax}{%
        \appto\tmp@tmprow{{\coltop} &}%
    }%
    \appto\tmp@tmprow{{\coltop}}%
    \tmp@tmprow \\[-1.2em]
    \toprule
}
\newcommand{\stTableEnd}[1]{%
    \\[-2.4em]%
    \def\tmp@tmprow{}%
    \Multido{}{\number\numexpr #1-1\relax}{%
        \appto\tmp@tmprow{{\colbottom} &}%
    }%
    \appto\tmp@tmprow{{\colbottom}}%
    \tmp@tmprow \\
    \bottomrule
}
%TODO: retrieve number of columns, then the parameter is not needed anymore
\newenvironment{strikethroughtable}[3][c]{% [#1: align], #2: col spec, #3: num cols
    \edef\strikethroughtable@numberofcolumns{#3}%
    \begin{tabular}[#1]{A #2 Z}%
        \stTableStart{\strikethroughtable@numberofcolumns}%
}{%
        \stTableEnd{\strikethroughtable@numberofcolumns}%
    \end{tabular}%
    \setLinesToCross{}%
    \setColsToCross{}%
}
\makeatother


% ========== init ==========
\strikethroughColumnStylePerpendicular
\setLinesToCross{}
\setColsToCross{}

main.tex:

\documentclass{article}

\input{table-strikethrough}
\usepackage{siunitx}

\usepackage{tikz}

\newcolumntype{m}[1]{ >{$} #1 <{$}}
\newcolumntype{~}{!{\quad}}

\begin{document}
\setColsToCross{5,7}
\strikethroughColumnStyleDiagonal
\begin{strikethroughtable}{mr @{} S[table-format=3.0]  *6{S[table-format=2.0]}}{8}
    \multicolumn{2}{c}{} & \multicolumn{1}{c}{$m_1=75$} & \multicolumn{1}{c}{$m_2=100$} & \multicolumn{1}{c}{$m_3=50$} & \multicolumn{1}{c}{$m_4=125$} & \multicolumn{1}{c}{$m_5=85$} & \multicolumn{1}{c}{$m_6=145$} \\
    \midrule
    a_1={} & 85 &   0 &  0 & 0 & 10 &  0 & 25 \\
    a_2={} & 95 &   0 &  0 & 0 & 20 &  0 & 35 \\
    a_3={} &100 &   0 &  0 & 0 & 25 &  0 & 40 \\
    a_4={} &150 &  25 & 50 & 0 & 75 & 35 & 90 \\
    a_5={} &140 &  15 & 40 & 0 & 65 & 25 & 80 \\
    a_6={} & 60 &   0 &  0 & 0 &  0 &  0 &  0 \\
\end{strikethroughtable}
\end{document}

The two files should be located in the same directory.

enter image description here

I have formatted the table in compliance with the recommendations from the booktabs package (section 2 The layout of formal tables, page 3):

You will not go far wrong if you remember two simple guidelines at all times:

  1. Never, ever use vertical rules.
  2. Never use double rules.

Also I have used siunitx' S column type to align the numbers.

You can customize the visual appearance by redefining the following commands:

  • \strikethroughcolor
  • \strikethroughwidth
  • \strikethroughCrossWidth

or calling the following commands:

  • \strikethroughColumnStylePerpendicular
  • \strikethroughColumnStyleDiagonal
  • \strikethroughColumnStyleCounterdiagonal
  • \strikethroughColumnStyleCross

I have seen your comment that you just want to cross out the column $m_3=50$ but I have crossed out one more just to show how easy it would be to cross out more.

Please note that I have splitted up the first column in two to align the numbers therefore the column indices differ from what one might expect at first glance.

Adding something visual to every row (like with \newcolumntype{=}{@{${}={}$}} which would have been great here) would not deliver the desired output because I am adding an invisible row above and below where it would then appear, too.

jakun
  • 5,981
0

enter image description here

\documentclass{article}
\usepackage{tikz,array,xparse,luacode,tabularx}

\usetikzlibrary{tikzmark,calc,patterns}
\newcounter{TikzTabCount}

\makeatletter
\NewDocumentCommand{\TikzTabEnd}{O{\NbCols}}{%
    \\[-\ht\@arstrutbox]%
    \Row{#1}\stepcounter{TikzTabCount}%
    \\[-\dp\@arstrutbox]}
\makeatother

\ExplSyntaxOn
\NewDocumentCommand{\CountColumns}{m}
 {% count the number of & tokens in \@preamble
  \regex_count:nvN { \cT\& } { @preamble } \l_tmpa_int
  % they're one less than the columns
  \cs_gset:Npx #1 { \int_eval:n { 1 + \l_tmpa_int } }
 }
\cs_generate_variant:Nn \regex_count:nnN { nv }
\ExplSyntaxOff

\NewDocumentEnvironment{TikzTab}{md<>O{c}mO{\NbCols}}{%
    \IfNoValueTF{#2}{%
        \setcounter{TikzTabCount}{2}%
        \begin{#1}[#3]{#4}%
        }{%
        \tabularx{#2}[#3]{#4}%
    }%
    \noalign{\CountColumns{#5}\tikzmark[yshift=5pt]{TTtop}}%
}{%
    \IfNoValueTF{#2}{%
        \end{#1}%
        }{%
        \endtabularx%
        }%
    \foreach \i in {0,...,4} {%
        \tikz[overlay,remember picture]
        \tikzmark{A\i}{({pic cs:TTtop}-|{pic cs:B\i})};
    }
}

\newcommand{\Row}[1]{\directlua{row(#1)}}

\begin{luacode}
function row (s)
    a = "\\multicolumn{1}{@{}c@{}}{\\tikzmark{\\Alph{TikzTabCount}0}"
    for i = 1,s-1
    do
    a=a.."\\strut\\hfill\\tikzmark{\\Alph{TikzTabCount}"..i.."}}&\\multicolumn{1}{@{}c@{}}{"
    end
    a=a.."\\strut\\hfill\\tikzmark{\\Alph{TikzTabCount}"..s.."}}"
    tex.print (a)
    --print (a)
end
\end{luacode}


\begin{document}

%\tikz[overlay,remember picture]
%   \fill[blue!10] (pic cs:A0) rectangle (pic cs:B4) ;

%\tikz[overlay,remember picture]
%   \fill[green!20] [rounded corners=8pt] (pic cs:B2) 
%       -- (pic cs:C2) -- (pic cs:C4) [rounded corners=0pt]
%       -- (pic cs:B4) -- cycle  ;

\foreach \j in {A,B,C} {%
\foreach \i in {0,...,4} {%
    \tikz[overlay,remember picture]
        \node[red!50] at (pic cs:\j\i) {\tiny \j\i} ;
}}


$\begin{TikzTab}{array}{|c|c|cm{2cm}|}\hline
    \rule{0pt}{1.5em}a&z&a&d\TikzTabEnd\hline
    \rule[-.5em]{0pt}{1.5em}a&z&a&d\pi\TikzTabEnd\hline
\end{TikzTab}$

\tikz[overlay,remember picture]
        \draw[blue] (pic cs:A0) -- (pic cs:C1)
    ($(pic cs:B0)!.5!(pic cs:C0)$) -- ($(pic cs:B4)!.5!(pic cs:C4)$) ;

\tikz[overlay,remember picture]
    \fill[pattern=north west lines, pattern color=blue]
    (pic cs:A1) rectangle (pic cs:C2);

\end{document}
Tarass
  • 16,912
0

Inspired from @Caramdir's answer for embedding a table inside a tikzpicture.

Wrapping the table in tikzpicture and treat it as a node gives you access to tikz's bag of tricks.

This approach is kinda gimmicky (absolute coordinates, ampersand replacement, ...). but it got the job done for my use case.

\documentclass{standalone}
\usepackage{tikz}

\begin{document} \begin{tikzpicture} \matrix[ampersand replacement=&amp;] { \node (sample) [] { \begin{tabular}{cc|c|c|c|} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{} & \multicolumn{1}{c}{2''} &amp; \multicolumn{1}{c}{} \\ &amp; \multicolumn{1}{c}{} &amp; \multicolumn{1}{c}{j} &amp; \multicolumn{1}{c}{k} &amp; \multicolumn{1}{c}{l} \\ \cline{3-5} &amp; g &amp; 75, 25 &amp; 57, 42 &amp; 66, 32 \\ \cline{3-5}1'' & h & 80, 26 & 35, 12 & 32, 54 \ \cline{3-5} & i & 28, 27 & 63, 31 & 54, 29 \ \cline{3-5} \end{tabular} }; \draw[dashed, blue] (-1.9, 0) -- (3.2, 0); \draw[dashed, blue] (-0.6, 0.7) -- (-0.6, -1.5); \draw[dashed, blue] (-1.9, -0.5) -- (3.2, -0.5); \draw[dashed, blue] (2.25, 0.7) -- (2.25, -1.5); \ }; \end{tikzpicture}

\end{document}

sample.png

Mahmoud
  • 101
0

The environment {NiceTabular} of nicematrix is similar to the standard environment {tabular} (as defined by array) but adds some functionnalities and creates PGF/TikZ nodes under the cells, rows and columns. Then it's possible to use TikZ to draw whatever you want using those nodes.

\documentclass{article}
\usepackage{geometry}
\usepackage{siunitx}
\usepackage{nicematrix,tikz}
\usetikzlibrary{patterns}
\usepackage{booktabs}

\begin{document}

\begin{NiceTabular}{lS[table-format=3.0]*6{S[table-format=2.0]}} \toprule & {$m_1=75$} & {$m_2=100$} & {$m_3=50$} & {$m_4=125$} & {$m_5=85$} & {$m_6=145$} & {$m_7=95$}\ \midrule $a_1={}$ & 85 & 0 & 0 & 0 & 10 & 0 & 25 \ $a_2={}$ & 95 & 0 & 0 & 0 & 20 & 0 & 35 \ $a_3={}$ &100 & 0 & 0 & 0 & 25 & 0 & 40 \ $a_4={}$ &150 & 25 & 50 & 0 & 75 & 35 & 90 \ $a_5={}$ &140 & 15 & 40 & 0 & 65 & 25 & 80 \ $a_6={}$ & 60 & 0 & 0 & 0 & 0 & 0 & 0 \ \bottomrule \CodeAfter \tikz \draw [red,thick] (1-|3) -- (last-|4) (last-|3) -- (1-|4) ; \tikz \draw [blue,thick] (1-|6.5) -- (last-|6.5) ; \tikz \fill [pattern = north east lines] (1-|8) rectangle (last-|9) ; \end{NiceTabular}

\end{document}

Output of the above code

F. Pantigny
  • 40,250