3

How can I produce the following graph in tikz:

enter image description here

  • It is basically produced with segment lines that can be drawn with \draw (x,y) -- (z,w); where x,y,z,w are the coordinates with the points. Also options should be used to change the line style, for example \draw[dashed] – Sigur Aug 17 '13 at 23:33
  • It is just the floating ranges that I don't see how to make. – Loie Benedicte Aug 17 '13 at 23:35
  • General solution would be nice if it was explained. – Loie Benedicte Aug 17 '13 at 23:36
13

That’s quite easy, I’ll show a way using different “varaibles”. This make the code a little more complex but also more flexible.

Step 1
Set up the basic environment: load the package and the arrows and calc library:

\documentclass{article}% ... or whatever you like

\usepackage{tikz}
\usetikzlibrary{arrows,calc}

\begin{document}
\begin{tikzpicture}
   % picture commands
\end{tikzpicture}
\end{document}

Every thing that follows goes inside the {tikzpicture}.

Step 2
Set up the variables. We’ll set up the vectors as coordinates, starting from the origin.

\coordinate (A) at (3,0);
\coordinate (B) at (0,-2);

and the upper left edge of the cross product

\coordinate (cross prod) at (1,4);

We add a simply \def to store the length of the ticks

\def\tick{0.1}

Step 3
Draw the axes: Use the \draw command with the option -> to add the arrow tips. We us a node with some options at the end of the path to add the labels.

\draw [->] (-1,0) -- (6,0) node [below] {$x$};
\draw [->] (0,-1) -- (0,5) node [left]  {$y$};

Then we use a \foreach loop to add the ticks.

\foreach \x in {1,...,5}
   \draw (\x,\tick/2) -- ++(0,-\tick) node [below] {\x};
\foreach \y in {1,...,4}
   \draw (\tick/2,\y) -- ++(-\tick,0) node [left] {\y};

Step 4
Draw the vectors. We define a style to make all vectors look the same and add it as option to {tikzpicture}. The vectors should be thick, and have “arrow” tips on both sides ([-))

\begin{tikzpicture}[
   vec/.style={thick,[-)},
]

Now we can draw the vectors. Starting at a certian point and then using the ++ syntax to add the components of the vector. Again we use a node to add the names.

\draw [vec] (1,-1) -- ++(A) node [midway,below] {$A$};
\draw [vec] (-1,4) -- ++(B) node [midway,left]  {$B$};

Step 5

Now draw the cross product. First we draw the filling in gray using the \fill macro and the rectangle path operation.

\fill [gray] (cross prod) rectangle ++($(A)+(B)$);

An then the borders and the label using \draw again. The -| path operation allows the automatic creation of the right angle.

\draw [thick] ($(cross prod)+(A)$) -| ($(cross prod)+(B)$);
\draw [thick,dashed] ($(cross prod)+(A)$) |- ($(cross prod)+(B)$)
   node [pos=0.25,right] {$A \times B$};

And that’s it.

cross product

full code

\documentclass{article}% ... or whatever you like

\usepackage{tikz}
\usetikzlibrary{arrows,calc}

\begin{document}
\begin{tikzpicture}[
    vec/.style={thick,[-)},
]
    % Step 2
    \coordinate (A) at (3,0);
    \coordinate (B) at (0,-2);
    \coordinate (cross prod) at (1,4);
    \def\tick{0.2}
    % Step 3
    \draw [->] (-1,0) -- (6,0) node [below] {$x$};
    \draw [->] (0,-1) -- (0,5) node [left]  {$y$};
    \foreach \x in {1,...,5}
        \draw (\x,\tick/2) -- ++(0,-\tick) node [below] {\x};
    \foreach \y in {1,...,4}
        \draw (\tick/2,\y) -- ++(-\tick,0) node [left] {\y};
    % Step 4
    \draw [vec] (1,-1) -- ++(A) node [midway,below] {$A$};
    \draw [vec] (-1,4) -- ++(B) node [midway,left]  {$B$};
    % Step 5
    \fill [gray] (cross prod) rectangle ++($(A)+(B)$);
    \draw [thick] ($(cross prod)+(A)$) -| ($(cross prod)+(B)$);
    \draw [thick,dashed] ($(cross prod)+(A)$) |- ($(cross prod)+(B)$)
        node [pos=0.25,right] {$A \times B$};
\end{tikzpicture}
\end{document}

update

with some adjustments this will work with every two vectors.

cross product II

\documentclass{article}% ... or whatever you like

\usepackage{tikz}
\usetikzlibrary{arrows,calc}

\begin{document}
\begin{tikzpicture}[
    vec/.style={thick,[-)},
]
    % Step 2
    \coordinate (A) at (3,-1);
    \coordinate (B) at (-0.5,-2);
    \coordinate (cross prod) at (1,4);
    \def\tick{0.2}
    % Step 3
    \draw [->] (-1,0) -- (6,0) node [below] {$x$};
    \draw [->] (0,-1) -- (0,5) node [left]  {$y$};
    \foreach \x in {1,...,5}
        \draw (\x,\tick/2) -- ++(0,-\tick) node [below] {\x};
    \foreach \y in {1,...,4}
        \draw (\tick/2,\y) -- ++(-\tick,0) node [left] {\y};
    % Step 4
    \draw [vec] (1,-1) -- ++(A) node [midway,below] {$A$};
    \draw [vec] (-1,4) -- ++(B) node [midway,left]  {$B$};
    % Step 5
    \fill [gray] (cross prod) -- +(A) -- +($(A)+(B)$) -- +(B);
    \draw [thick] ($(cross prod)+(A)$) -- (cross prod) -- ($(cross prod)+(B)$);
    \draw [thick,dashed] ($(cross prod)+(A)$) -- ($(cross prod)+(A)+(B)$)
        node [midway,right] {$A \times B$} -- ($(cross prod)+(B)$);
\end{tikzpicture}
\end{document}

this one uses the + syntax which means that the coordinate is realtive but without setting it as new reference point, as the ++ does.

| improve this answer | |
4

If you are using this many times, you may like to create a macro. This is to illustrate how to convert Tobi's excellent code into one.

\documentclass{article}% ... or whatever you like

\usepackage{tikz}
\usetikzlibrary{arrows,calc}

\newcommand\crossprod[4][5]{%
\begin{tikzpicture}[
    vec/.style={thick,[-)},
]
    % Step 2
    \coordinate (A) at #3;
    \coordinate (B) at #4;
    \coordinate (cross prod) at #2;
    \def\tick{0.2}
    % Step 3
    \draw [-stealth] (-1,0) -- ($(#1,0)+(0.5,0)$) node [below] {$x$};
    \draw [-stealth] (0,-1) -- ($(0,#1)+(0,0.5)$) node [left]  {$y$};
    \foreach \x in {1,...,#1}
        \draw (\x,\tick/2) -- ++(0,-\tick) node [below] {\x};
    \foreach \y in {1,...,#1}
        \draw (\tick/2,\y) -- ++(-\tick,0) node [left] {\y};
    % Step 4
    \draw [vec] (1,-1) -- ++(A) node [midway,below] {$A$};
    \draw [vec] (-1,4) -- ++(B) node [midway,left]  {$B$};
    % Step 5
    \fill [gray!30] (cross prod) -- +(A) -- +($(A)+(B)$) -- +(B);
    \draw [thick] ($(cross prod)+(A)$) -- (cross prod) -- ($(cross prod)+(B)$);
    \draw [thick,dashed] ($(cross prod)+(A)$) -- ($(cross prod)+(A)+(B)$)
        node [midway,right] {$A \times B$} -- ($(cross prod)+(B)$);
\end{tikzpicture}
}

%% #1 -- Optional argument for length of x and y axes; default is 5. Adjust for suitability
%% #2 -- upper left edge of the cross product. Adjust for suitability
%% #3 -- vector A as coordinate, starting from the origin.
%% #4 -- vector B as coordinate, starting from the origin.

\begin{document}
\crossprod{(1,5)}{(3,0)}{(0,-2)}

\crossprod[6]{(2,5)}{(3,-2)}{(-0.5,-2)}
\end{document}
| improve this answer | |
2

This is a simple code:

\begin{tikzpicture}[font={\small}]
\draw[-latex] (-0.5,0) -- (5.5,0) node[below] {$x$};
\draw[-latex] (0,-0.5) -- (0,5.5) node[left] {$y$};
\foreach \x in {1,2,3,4,5}
{
\draw (0.1,\x) -- (-0.1,\x) node[left] {$\x$};
\draw (\x,0.1) -- (\x,-0.1) node[below] {$\x$};
}
\path[fill=lightgray] (1,2) rectangle +(3,2);
\draw[thick] (1,2) -- (1,4) -- (4,4);
\draw[thick,dashed] (1,2) -- (4,2) -- (4,4) node[right,midway] {$A\times B$};
\draw[very thick] (1,-1) node {\small [} -- (4,-1) node {)} node[below,midway] {$A$};
\draw[very thick] (-1,2) node[rotate=90] {[} -- (-1,4) node[rotate=90] {)}
node[left,midway] {$B$};
\end{tikzpicture}
| improve this answer | |

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.