4

I'm trying to strike out a column and a row from a matrix, and I get it to work if I define the tikzmark myself (from I post I found on the internet). I understand, however, that tikzmark is now included by default in the tikz library, so I tried to use it instead. But I get errors of names not defined. This is the MWE:

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

%% draw horizontal and vertical lines in tables
%\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}

\newcommand{\DrawVLine}[3][]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[#1] (#2.north) -- (#3.south);
  \end{tikzpicture}
}

\newcommand{\DrawHLine}[3][]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[#1] (#2.west) -- (#3.east);
  \end{tikzpicture}
}

\begin{document}
\begin{equation*}
\begin{bmatrix}
 \tikzmark{top}{ a } & 0 & -a & \tikzmark{right}{-c} \\
 0 & b & 0 & -b \\ 
 -a & 0 & a & k \\
 \tikzmark{bottom}{d} & -b  & d & c \\
\end{bmatrix} 
\end{equation*}
\DrawVLine[red, ultra thick, opacity=0.5]{top}{bottom}
\DrawHLine[red, ultra thick, opacity=0.5]{top}{right}

\end{document}

which doesn't work. But if you uncomment the definition of tikzmark and remove it from \usetikzlibrary then I get the expected result:

enter image description here

1
  • I guess you confuse \tikzmark with some \tikznode command, which is also around. Referring to coordinates from \tikzmark requires a pic cs:.
    – user121799
    Jul 8 '18 at 12:40
3

I think you're not using the tikzmark library correctly. First of all, the tikzmark command takes one mandatory argument, not two as in your commented out version, and one option. More importantly, to refer to coordinates you need to use pic cs:.

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

%% draw horizontal and vertical lines in tables
%\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}

\newcommand{\DrawVLine}[3][]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[#1] (pic cs:#2) -- (pic cs:#3);
  \end{tikzpicture}
}

\newcommand{\DrawHLine}[3][]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[#1] (pic cs:#2) -- (pic cs:#3);
  \end{tikzpicture}
}

\begin{document}
\begin{equation*}
\begin{bmatrix}
 \tikzmark{top}a & 0 & -a & -c\tikzmark{right} \\
 0 & b & 0 & -b \\ 
 -a & 0 & a & k \\
 \tikzmark{bottom}d & -b  & d & c \\
\end{bmatrix} 
\end{equation*}
\DrawVLine[red, ultra thick, opacity=0.5]{top}{bottom}
\DrawHLine[red, ultra thick, opacity=0.5]{top}{right}

\end{document}

enter image description here

As you see, this does work, but yes, the output is not optimal. The perhaps best way to go is to call your \tikzmark command \tikznode and then use it as you suggest (with the baseline option, as in the \tikznode commands that you can find e.g. here).

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}

\newcommand{\tikznode}[2]{%
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}

%% draw horizontal and vertical lines in tables
%\newcommand{\tikzmark}[2]{\tikz[overlay,remember picture,baseline] \node [anchor=base] (#1) {$#2$};}

\newcommand{\DrawVLine}[3][]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[#1] (#2.north) -- (#3.south);
  \end{tikzpicture}
}

\newcommand{\DrawHLine}[3][]{%
  \begin{tikzpicture}[overlay,remember picture]
    \draw[#1] (#2.west) -- (#3.east);
  \end{tikzpicture}
}

\begin{document}
\begin{equation*}
\begin{bmatrix}
 {}\tikznode{top}{a} & 0 & -a & {}\tikznode{right}{-c} \\
 0 & b & 0 & -b \\ 
 -a & 0 & a & k \\
 {}\tikznode{bottom}{d} & -b  & d & c \\
\end{bmatrix} 
\end{equation*}
\DrawVLine[red, ultra thick, opacity=0.5]{top}{bottom}
\DrawHLine[red, ultra thick, opacity=0.5]{top}{right}
\end{document}

enter image description here

Let me stress that the \tikzmark command is rather sophisticated as it discriminates between being called inside or outside a tikzpicture.

3
  • Do you think it's better I just use my tikzmark definition? The output is optimal in this case.
    – aaragon
    Jul 8 '18 at 12:51
  • @aaragon I added what I would do. Notice that because of some amsmath hacks one has to use {} before the \tikznodes, which is not necessary under "normal" circumstances, where \tikznode detects the math mode and typesets accordingly. If you don't want to have the darker color where the lines intersect, you can work with transparency groups, but this would require rewriting your macros.
    – user121799
    Jul 8 '18 at 12:58
  • 1
    @aaragon And yes, I also like the output of your definition, I just would not call the macro \tikzmark as you may want to use the "real" \tikzmark in parallel.
    – user121799
    Jul 8 '18 at 13:00
2

I personally it'd be simpler to use the \matrix environment in Tikz, where you can assign a name to the whole matrix and, in turn, to the nodes.

Output

highlighted matrix picture

Code

\documentclass[margin=1cm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}

\newcommand{\DrawV}[3][]{%
    \draw[#1, line width=3mm, opacity=.2] (#2.north) -- (#3.south);
}

\newcommand{\DrawH}[3][]{%
    \draw[#1, line width=3mm, opacity=.2] (#2.west) -- (#3.east);
}


\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,left delimiter={[}, right delimiter={]}] {%
    a & 0 & -a & -c \\
    0 & b & 0 & -b \\ 
   -a & 0 & a & k \\
    d & -b & d & c \\
};

\DrawV[red]{m-1-1}{m-4-1}
\DrawH[green]{m-1-2}{m-1-4}
\end{tikzpicture}
\end{document}
2
  • Thanks for your answer. I can't actually use that because I need to use a blockarray to also number the indexes of the matrix.
    – aaragon
    Jul 8 '18 at 12:42
  • @aaragon Sorry, I rarely if ever use matrices, but do you mean indexing (i.e. adding sort of headings to) the singular rows and columns or the matrix itself?
    – Alenanno
    Jul 8 '18 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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