7

I am trying to give a very simple analogy to what I am trying to do with LaTeX. I simply want to write two vectors, one below or next the other, and then, use nodes to link the last two elements of the first vector and the last element from the second vector. Here is the code I am using

\documentclass{article}
\usepackage{amsmath, amssymb}
\usepackage{tikz}
\usetikzlibrary{arrows}% For nice arrow tips
% One style for all TikZ pictures for working with overlays:
\tikzset{every picture/.style=remember picture}
% Define a TikZ node for math content:
\newcommand{\mathnode}[1]{%
  \mathord{\tikz[baseline=(#1.base), inner sep = 0pt]{\node (#1) {$#1$};}}}



\begin{document}

$\begin{bmatrix}
     1 \\ \mathnode{2\rho \pi \\ 3}
\end{bmatrix}$
\\
$\begin{bmatrix}
     1 \\ 2\rho \pi \\ \mathnode{3}
\end{bmatrix}$

% Now we draw connections between defined nodes:
\begin{tikzpicture}[overlay]
  \path [>=stealth, <->, shorten <= 3pt, shorten >=3 pt]
    (N1) edge [bend left=60] (N2);
\end{tikzpicture}


\end{document}

How can I modify the mathnode declaration, to give a name to the node, and to be able to fill each node separately ?

Please, any suggestions?

Thanks.

3
  • How should the connecting element look like? Can you show us an image (it could be a hand drawing) of the expected result? Jul 15, 2013 at 18:55
  • What are you trying to achieve exactly? You can’t use the \\ for a new matrix line in a node. You can’t/shouldn’t use \rho, \pi and \\ in a node name which is in both cases neither N1 nor N2. You are also missing remember picture. Jul 15, 2013 at 20:02
  • @Qrrbrbirlbel Thanks for the answer...below jLDiaz gave a good way to do that, but Isn't there a more robust way? select the shape and filling color of the node independently each time? Jul 15, 2013 at 20:27

2 Answers 2

10

I don't quite understand the output you have in mind, but I definitely would go through the tikz matrix way, by putting two matrices of nodes in the same tikzpicture, without using remember picture nor overlay.

For example:

\documentclass{article}
\usepackage{amsmath, amssymb}
\usepackage{tikz}
\usetikzlibrary{arrows,matrix,positioning}% For nice arrow tips
\begin{document}

\tikzset{
    !/.style = {
        fill=yellow!30,
    },
    mymatrix/.style = {
        matrix of math nodes,
        left delimiter  = (,
        right delimiter = ),
        nodes={minimum width=6ex},
    }
}

\begin{tikzpicture}
    \matrix[mymatrix, name=M1]{
                1 \\
        |[!]|   2\rho\pi \\
        |[!]|   3\\
    };
    \matrix[mymatrix, name=M2, below=of M1] {
                 1 \\
                 2\rho\pi\\
        |[!]|    3\\
    };
   \draw [red, >=stealth, <->, shorten <= 3pt, shorten >=3 pt]
     (M1-2-1.south east) to[bend left=60] (M2-3-1.east);
\end{tikzpicture}

\end{document}

Produces:

Result

6
  • The horizontal spacing is quite big. Why the minimum width? Jul 15, 2013 at 19:58
  • Thanks @jLDiaz for the answer. This is exactly what I was looking for, but can't we make something more robust, like the shape and filling color of the node, ar to be selected independently each time, and not fixed by the \tikset command. Jul 15, 2013 at 20:22
  • @user2536125 You can do that. You can define different styles besides ! or simply replace ! or mymatrix in the actual use with what you want. Anyway, I’d use a different input where you only mark the row which should be highlighted or something like that. You may update your question with the kind of “more robust” you had in mind. Jul 15, 2013 at 20:42
  • @Qrrbrbirlbel What do you mean replace ! Could you please give an example? This question I already included in the original thread. Jul 15, 2013 at 20:52
  • @user2536125 Well, just try |[circle,fill=red]| 2\rho\pi \\ for example. Jul 15, 2013 at 20:56
8

By means of hf-tikz, one isn't forced to exploit the TikZ-matrix. This is an example showing how to perform such a task:

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath, amssymb}
\usepackage[customcolors,markings]{hf-tikz}
\usetikzlibrary{calc}

\tikzset{aspect/.style={
    set fill color=green!50!lime!60,
    set border color=white,
    disable rounded corners=true
  },
  vert offsets/.style={
    above offset=0.32,
    below offset=-0.08
  },
  expr/.style={
    left offset=-0.05,
    right offset=0.25,
    vert offsets,
    aspect,
  },
  number/.style={
    left offset=-0.1,
    right offset=0.1,
    vert offsets,
    aspect
  }
}

\begin{document}
\[\begin{bmatrix}
     1 \\ \tikzmarkin[expr, mark at=0.135]{a}2\rho \pi \\ 3\tikzmarkend{a}
\end{bmatrix}
\]
% store the point in a coordinate: let it be a bit shifted for clarity
\tikz[remember picture,overlay]\coordinate[use marker id,xshift=0.1cm] (A) at (0,0);

\[\begin{bmatrix}
     1 \\ 2\rho \pi \\ \tikzmarkin[number, mark at=0.135]{b}3\tikzmarkend{b}
\end{bmatrix}
\]
% store the point in a coordinate: let it be a bit shifted for clarity
\tikz[remember picture]\coordinate[use marker id,xshift=0.25cm] (B) at (0,0);

% draw the curve
\tikz[remember picture,overlay]\draw[blue] 
 (A) edge[bend left=60,stealth-stealth](B);
\end{document}

The result:

enter image description here

The markings option is a feature of version 0.3 of the package, highly based on the nice answer Jake gave in How to draw tangent line of an arbitrary point on a path in TikZ. It exploits the TikZ markings library, thus it is possible to deploy markers as a number from 0 (start of the path) to 1 (end of the path). To show where the markers are placed, use the show markers key; for example:

\[\begin{bmatrix}
     1 \\ \tikzmarkin[expr, show markers, mark at=0.135]{a}2\rho \pi \\ 3\tikzmarkend{a}
\end{bmatrix}
\]

...

\[\begin{bmatrix}
     1 \\ 2\rho \pi \\ \tikzmarkin[number,show markers, mark at=0.135]{b}3\tikzmarkend{b}
\end{bmatrix}
\]

gives:

enter image description here

Moreover, the package allows to define aspects style in terms of shading: they can be adopted separately each time one highlights a formula. One way to use them, is the following:

  • define some general aspects style, say aspect x;
  • define styles containing offset definition plus an argument: this will be in charge to select the proper aspect style; an example: expr=aspect x.

This is an example covering some of the possibilities:

\documentclass[11pt,a4paper]{article}
\usepackage{amsmath, amssymb}
\usepackage[customcolors,shade,markings]{hf-tikz}
\usetikzlibrary{calc}

\tikzset{aspect 1/.style={
    set border color=green!70!black,
    top color=white,
    bottom color=green!50!lime!60,
    disable rounded corners=true
  },
  aspect 2/.style={
    set border color=cyan!50!blue,
    bottom color=white,
    top color=cyan!50!blue!60,
    disable rounded corners=true
  },
  aspect 3/.style={
    set border color=white,
    inner color=white,
    outer color=red!75!black!30,
    disable rounded corners=true
  },  
  vert offsets/.style={
    above offset=0.32,
    below offset=-0.08
  },
  expr/.style={
    left offset=-0.05,
    right offset=0.25,
    vert offsets,
    #1,
  },
  number/.style={
    left offset=-0.1,
    right offset=0.1,
    vert offsets,
    #1
  }
}

\begin{document}
\[
\begin{bmatrix}
     1 \\ \tikzmarkin[expr=aspect 1, mark at=0.135]{a}2\rho \pi \\ 3\tikzmarkend{a}
\end{bmatrix}
% store the point in a coordinate: let it be a bit shifted
\tikz[remember picture,overlay]\coordinate[use marker id,xshift=0.1cm] (A) at (0,0);
\hspace{1cm}
\begin{bmatrix}
     1 \\ \tikzmarkin[expr=aspect 2, mark at=0.135]{a2}2\rho \pi \\ 3\tikzmarkend{a2}
\end{bmatrix}
% store the point in a coordinate: let it be a bit shifted
\tikz[remember picture,overlay]\coordinate[use marker id,xshift=0.1cm] (A2) at (0,0);
\hspace{1cm}
\begin{bmatrix}
     1 \\ \tikzmarkin[expr=aspect 3, mark at=0.135]{a3}2\rho \pi \\ 3\tikzmarkend{a3}
\end{bmatrix}
% store the point in a coordinate: let it be a bit shifted
\tikz[remember picture,overlay]\coordinate[use marker id,xshift=0.1cm] (A3) at (0,0);
\]


\[
\begin{bmatrix}
     1 \\ 2\rho \pi \\ \tikzmarkin[number=aspect 3, mark at=0.135]{b}3\tikzmarkend{b}
\end{bmatrix}
% store the point in a coordinate: let it be a bit shifted
\tikz[remember picture]\coordinate[use marker id,xshift=0.25cm] (B) at (0,0);
\hspace{1cm}
\begin{bmatrix}
     1 \\ 2\rho \pi \\ \tikzmarkin[number=aspect 1, mark at=0.135]{b2}3\tikzmarkend{b2}
\end{bmatrix}
% store the point in a coordinate: let it be a bit shifted
\tikz[remember picture]\coordinate[use marker id,xshift=0.25cm] (B2) at (0,0);
\hspace{1cm}
\begin{bmatrix}
     1 \\ 2\rho \pi \\ \tikzmarkin[number=aspect 2, mark at=0.135]{b3}3\tikzmarkend{b3}
\end{bmatrix}
% store the point in a coordinate: let it be a bit shifted
\tikz[remember picture]\coordinate[use marker id,xshift=0.25cm] (B3) at (0,0);
\]


% draw the curve
\tikz[remember picture,overlay]\draw[cyan!50!blue] 
 (A) edge[bend left=60,stealth-stealth](B);
\tikz[remember picture,overlay]\draw[red!75!black] 
 (A2) edge[bend left=60,stealth-stealth](B2);
\tikz[remember picture,overlay]\draw[green!70!black] 
 (A3) edge[bend left=60,stealth-stealth](B3);
\end{document}

The result:

enter image description here

2
  • thanks for the reply. But what happens if I am working in TikzEdt? Jul 16, 2013 at 7:11
  • To be honest: I don't know, give it a try :) Anyway, the answers, here, are not only target to solve OP's problem, but for a general audience: maybe it won't help you for some TikZEdt issues, but it can be useful to someone else using other editors. Then, if you use TikZEdt just to draw the image and then bring it to a standard document, here you have another way to solve the problem directly in the document. Jul 16, 2013 at 7:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .