Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am writing a technical document, and I need to explain several steps of a construction which use matrices and linear equations. I was puzzled how I would create my figures and one paper I reference use this : how a step look like

I have to do 6 or 7 of these, and I would like to know what is the most efficient way of doing it. In each figure the number of components and the value of equation will be different. I already know how to create individually matrices and tables. I want to know how to produce such a figure without too much sweat. I don't use latex a lot, but if you point out packages, references or any material to look at it will be a good start.

share|improve this question
add comment

1 Answer

up vote 13 down vote accepted

You can use TikZ for that, but I’m not sure if this is the most efficient way, since I don’t understand how your images will differ from each other. So you only want to change some numbers in the equations?

\documentclass{article}

\usepackage{tikz}
% load extra stuff
\usetikzlibrary{shapes.misc,matrix,positioning,fit}
% some settings and style definitions
\tikzset{
    boxed eqns/.style={% 
        matrix of math nodes,
        row sep=-0.4pt,
        nodes={
            draw,
            minimum width=1cm,
            minimum height=0.6cm,
        },
    },
    values/.style={
        matrix of math nodes,
        left delimiter=[,
        right delimiter=],
    },
    equation/.style={
        execute at begin node={$\displaystyle},
        execute at end node={$},
    },
    blue box around/.style={
        draw,
        ultra thick,
        blue,
        fit=#1,
        inner sep=0pt,
    },
    cancel/.style={
        cross out,
        draw,
        red,
        ultra thick,
        fit=#1,
        inner xsep=10pt,
        inner ysep=1pt,
    },
}

\begin{document}
\begin{tikzpicture}
    \matrix (Variables) [boxed eqns] {a_1\\a_2\\a_3\\};
    \matrix (Values) [values,right=of Variables] {1\\2\\3\\};
    \node (Eqn) [equation,right=of Values] {a_1+a_2+a_3};
    \foreach \l in {1,2,3}
        \draw [->] (Variables-\l-1) -- (Eqn);
\end{tikzpicture}

\begin{tikzpicture}
    \matrix (Variables) [boxed eqns] {a_1\\a_2\\a_3\\};
    \matrix (Values) [values,right=of Variables] {1\\2\\3\\};
    \node [blue box around=(Variables-2-1)] {};
    \node [cancel=(Values)] {};
\end{tikzpicture}
\end{document}

result

How to get the upper picture?

  1. Create the styles for different parts of your images.

    • I used matrixes, that switch to math mode for their content
    • I let the matrices draw the frames and delimiters
  2. Set up the {tikzpicture} environment.

  3. Draw the first matrix with the boxed variables.

    • Us the boxed eqns style.
    • Give a name to it in braces, e.g. (Variables).
    • Use \\ after each variable, even the last one.
  4. Draw the second matrix with the values.

    • Us the values style.
    • Give a name to it in braces, e.g. (Values).
    • Use \\ after each value, even the last one.
    • Postion it right of the first on with right=of Variables.
  5. Draw the node containing the equantion.

    • Us the equation style.
    • Give a name to it in braces, e.g. (Eqn).
    • Postion it right of the first on with right=of Values.
  6. Draw the arrows

    • Use \foreach to repeat the things for every item of the list {1,2,3}, and save the current item in \l.
    • Set up the \draw command with -> to get an arrow tip at the end.
    • Begin the path at (Variables-\l-1), which is one of the first matrix’ cell.
    • End the path at (Eqn). The best end position is calculated automatically.

How to get the lower picture?

  1. Draw the matrices as above.

  2. Use blue box around=(<node name>) to frame a node or part of a matrix*.

  3. Use cancel=(<node name>) to put a cross over a node or part of a matrix*.

* The part of a matrix is a node …

How to draw an equation system?

Add this definition to \tikzsset

eqn system/.style={
    matrix of math nodes,
    nodes in empty cells,
    row sep=0.1cm,
    column sep=0.5cm,
    draw,
    nodes={
        anchor=base east,
    },
},

and use it like this:

\begin{tikzpicture}
    \matrix (ES) [eqn system] {
        a_1   & + 2a_2 &        & + 4a_4 \\
        10b_1 &        &        & -3b_a  \\
              & -  c_2 & + 3c_3 &        \\
    };
    \node [blue box around=(ES-1-1) (ES-1-4)] {};
\end{tikzpicture}

result 2

But this needs some improvements as you can see in the image …

Update

You could automat the things with a new macro

% \autodraw{<Varaibles>}{<Number of Variables>}{<Values>}{<Eqn>}
\newcommand{\autodraw}[4]{%
    \begin{tikzpicture}
        \matrix (Variables) [boxed eqns] {#1};
        \matrix (Values) [values,right=of Variables] {#3};
        \node (Eqn) [equation,right=of Values] {#4};
        \foreach \l in {1,...,#2}
            \draw [->] (Variables-\l-1) -- (Eqn);
    \end{tikzpicture}
}

example

\autodraw{b_1\\b_2\\b_2\\}{3}{7\\2\\5\\}{2b_1+b_2-3b_3}
share|improve this answer
    
Wow this is powerful! Seems like a complete language by itself, like matlab. I modified your script with : \matrix (Variables) [boxed eqns] {a_1\\a_2\\a_3\\a_4\\a_5\\a_6\\a_7\\}; \matrix (Values) [values, right=of Variables-2-1] {1\\2\\3\\}; \node (Eqn) [equation, right=of Values] {a_1+a_2+a_3}; \foreach \l in {1,2,3} \draw [->] (Variables-\l-1) -- (Eqn); –  UmNyobe Apr 13 '12 at 14:12
    
yes I want to modify the content of Variables, Values and Eqn. My guess will be to write a macro to put the arguments at the right position. Is there a better way?? –  UmNyobe Apr 13 '12 at 14:18
    
@UmNyobe: Yes TikZ is very powerful and can do even more complex things like this. It’s a user level layer of PGF which was created to do graphics. Have a look at the pgfmanual, which includes some great tutorials to learn TikZ. –  Tobi Apr 13 '12 at 14:19
    
@UmNyobe: I added a command to draw the things. You surely find a better name that \autodraw for it ;-) –  Tobi Apr 13 '12 at 14:25
    
Thanks. I will try to stand on my own feet. –  UmNyobe Apr 13 '12 at 14:39
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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