Graphics, Figures & Tablestikzpicture inside alignat environment

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
tonyjnel
Posts: 11
Joined: Thu May 12, 2011 3:51 pm

tikzpicture inside alignat environment

Post by tonyjnel »

Hi,

I am making a matrix with a red line through the first column and row. I have code that works for making this matrix, but when I try to put it into the alignat environment I get an error. Here is the (working) code that draws the matrix:

Code: Select all

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz} 
\usetikzlibrary{matrix} 
\tikzstyle{every left delimiter}=[xshift=1ex]
\tikzstyle{every right delimiter}=[xshift=-1ex]
\begin{document}
\begin{alignat*}{3}
	C_{11}&=&(-1)^{1+1}
	\begin{tikzpicture}
		\matrix(m1)[matrix of nodes, left delimiter=|, right delimiter=|]
			{
				2&4&7\\
				6&0&3\\
				1&5&3\\
			};
		\draw[thick,red](m1-3-1.center)|-(m1-1-1.center)|-(m1-1-3.center);
	\end{tikzpicture}
	&=&&(-1)^{1+1}\left|\begin{array}{rr}
		0&3\\
		5&3
	\end{array}\right|
\end{alignat*}	
\end{document}
When I try to just put this into my aligned equations I get the following error: "Package pgfbasematrix Error: Single ampersand used with wrong catcode." Here is my (not working) code inside the alignat environment:

Code: Select all

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{tikz} 
\usetikzlibrary{matrix} 
\tikzstyle{every left delimiter}=[xshift=1ex]
\tikzstyle{every right delimiter}=[xshift=-1ex]
\begin{document}
\begin{alignat*}{3}
		C_{11}&=&(-1)^{1+1}
		\begin{tikzpicture}
			\matrix(m1)[matrix of nodes, left delimiter=|, right delimiter=|]
				{
					2&4&7\\
					6&0&3\\
					1&5&3\\
				};
			\draw[thick,red](m1-3-1.center)|-(m1-1-1.center)|-(m1-1-3.center);
		\end{tikzpicture}
		&=&&(-1)^{1+1}\left|\begin{array}{rr}
		0&3\\
		5&3
	\end{array}\right|
	\end{alignat*}	\end{document}
It was my understanding that you could simply embed tikz pictures within the math environment and I can't figure out why this issue is coming up. I am new to using tikz and I have been unable to find the solution in the manual. Thanks for your help.

-Tony

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tikzpicture inside alignat environment

Post by localghost »

The problem here is that the ampersand (&) is reserved as alignment character inside the alignat environment. But pgf/tikZ offers an option to replace the ampersands by a macro. And this option needs to be added to the matrix structure.

Code: Select all

\documentclass[12pt]{article}
\usepackage{mathtools}   % loads »amsmath«
\usepackage{tikz}
\usetikzlibrary{matrix}

\tikzstyle{every left delimiter}=[xshift=1ex]
\tikzstyle{every right delimiter}=[xshift=-1ex]

\begin{document}
  \begin{alignat*}{2}
    C_{11} &= (-1)^{1+1}
    \begin{tikzpicture}[baseline]
      \matrix (m1) [%
        matrix of math nodes,
        ampersand replacement=\&,
        left delimiter=|,
        right delimiter=|]
        {
          2 \& 4 \& 7 \\
          6 \& 0 \& 3 \\
          1 \& 5 \& 3 \\
        };
      \draw[thick,red](m1-3-1.center)|-(m1-1-1.center)|-(m1-1-3.center);
    \end{tikzpicture}
    &= & (-1)^{1+1}
    \left|\begin{array}{rr}
      0 & 3 \\
      5 & 3
    \end{array}\right|
  \end{alignat*}   
\end{document}
The mentioned feature is documented in Section 17.5 of the package manual.


Thorsten
Post Reply