2

Imagine you have a matrix defined as:

\matrix(nums)[matrix of nodes]
        {
        1 & 2 \\ 
        3 & 4 \\ 
        5 & 6\\
        };

and you want to use what's written in node 1-2 and write it in another matrix or someplace along your tikzpicture.

Is there a way to do it?

Is it possible to choose an element of a list using some technique?

4

You can always define a macro for the desired contents and use whenever you need it:

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{matrix}

\newcommand{\myelement}{4}

\begin{document}
Next matrix 
\begin{tikzpicture}[baseline]
\matrix(nums)[matrix of nodes, left delimiter=(, right delimiter=)]
{ 1 & 2\\ 3 & \myelement \\ 5 & 6\\};
\end{tikzpicture}
has \myelement{} in position 2,2.

\end{document}

enter image description here

1

If you're mixing math and LaTeX you should consider the sagetex package which utilizes an open source computer algebra system (CAS), Sage. That CAS needs to be installed on your machine or you can access it through a free Sagemath Cloud account. Consider the following code:

\documentclass{article}
\usepackage{sagetex}
\begin{document}
\begin{sagesilent}
M=matrix([[1,2],[3,4],[5,6]])
N = matrix([[M[1,1],0],[0,M[0,1]]])
\end{sagesilent}
For the matrix $M=\sage{M}$ the element in the $(2,1)$ position is  $\sage{M[1][0]}$.
The dot product of the first two row vectors is
$\sage{M[0][0]}\cdot \sage{M[1][0]}+\sage{M[0][1]}\cdot \sage{M[1][1]}=
\sage{M[0][0]*M[1][0]+M[0][1]*M[1][1]}$. The $2 \times 2$ diagonal matrix   $N$, with 
diagonal elements $(2,2)$ and $(1,2)$ is $\sage{N}$. In that case the matrix $M*N=
\sage{M*N}$.
\end{document}

The output is shown below: enter image description here

Notice: Sage refers to matrix elements starting with 0, rather than 1 hence the (1,1) element is in the (0,0) position in the matrix. You can access the math through a \sage{} command. That allows you to pick off elements as you see fit, no macro required. You can also have Sage calculate the matrix multiplication of M*N, which eliminates the possibility of mistakes. Having a CAS do the math eliminates careless mistakes and saves you from having to typeset matrices.

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.