0

I'm looking to create a square graph in TeX, to represent the unit ball for the infinity norm. I am trying to use the TikZ package but I am having trouble using the \draw command. I want to create something like this.

enter image description here

Any suggestions are greatly appreciated!

1
  • 6
    Welcome to TeX.SX! Please show us what you have tried already. It makes it much easier to help you if we know what packages you are using and if we have some working code to start from (i.e. your code should compile). Also, people are much more likely to help if you show that you have made an effort rather than expecting some one to do everything for you:) – user30471 Jan 19 '17 at 6:32
8

This is rather easy.

Get yourself the faboulus TikZ- and PGF-Manual and Tutorial and follow the instructions from page 30 to page 43, as I did.

EDIT placed the lables above the rectangle.

\documentclass[11pt]{article}

\usepackage{tikz}

\begin{document}
\noindent%
%% Manual page 29
\begin{tikzpicture}[scale=4.5]
  %% First, the axis, from -1.4 to 1.4 in both directions
  %% Manual page 30, top
  \draw (-1.4, 0) -- (1.4, 0);
  \draw (0, -1.4) -- (0, 1.4);
  %% 
  %% Draw a grid
  %% Manual page 33
  \draw[ultra thin, gray, step=.1cm] (-1.3,-1.3) grid (1.3,1.3);
  %% 
  %% Finally, draw the red rectangle from -1,-1 to 1,1
  %% Manual page 32
  \draw[ultra thick, red] (-1, -1) rectangle (1, 1);
  %% 
  %% Place the labels
  %% Manual 43
  \foreach \x/\xtext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1}
  \draw (\x cm,1pt) -- (\x cm,-1pt) node[anchor=north] {$\xtext$};
  \foreach \y/\ytext in {-1, -0.5/-\frac{1}{2}, 0.5/\frac{1}{2}, 1}
  \draw (1pt,\y cm) -- (-1pt,\y cm) node[anchor=east] {$\ytext$};
\end{tikzpicture}
\end{document}

As Stefan asked for the result---here it it:

enter image description here

7
  • 2
    The manual might be huge and a bit daunting, but it's also a fine example of a good, detailed manual -- better than anything I've seen in commercial software recently. – Chris H Jan 19 '17 at 13:17
  • 2
    You might be so impressed by its sheer size (> 1000 pages), that you might get scared. But it is very well taught and worth the read. The solution above is my third try in TikZ. In fact, it was just copy and paste. – Jan Jan 19 '17 at 13:20
  • 1
    A tip: when trying to learn about a common command, navigate to the start of the index, then hit search to find its real entry. Searching for \draw will get you hits on every page – Chris H Jan 19 '17 at 13:22
  • 1
    Which is no Problem, since all commands/Options/... are links. Just find the first \draw and click on it. – pschulz Jan 19 '17 at 14:08
  • @Jan, it would be nice, if you could add the resulting image of your code to your answer. – Stefan Pinnow Jan 19 '17 at 14:28
5

For graphs it is better to use pgfplots as it was specifically designed for graphing:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
        axis y line=center,
        axis x line=middle, 
        xmin=-1.5,
        xmax=1.5,
        ymin=-1.5,
        ymax=1.5,
        grid=both,
        minor tick num=4,
        axis equal image,
]
    \addplot[red, ultra thick] coordinates {(-1,1) (1,1) (1,-1) (-1,-1) (-1,1)};
\end{axis}
\end{tikzpicture}
\end{document}
2
  • There's a bad connection at (–1,1) – egreg Jan 19 '17 at 14:19
  • I have some minor suggestions, to simplify your code a bit: Instead of using two different values (which are aliases) you should provide either center or middle; even better would be to just use axis lines=... to directly set both axis. Then I would not repeat the last coordinate but use --cycle instead which should be used between the closing curly brace and the semicolon. Last I would define a variable using /tikz/declare function or \pgfmathsetmacro to store the axis limit and use this which makes it easier if you want to change it. – Stefan Pinnow Jan 19 '17 at 14:21
3

I present here just "a bit improved" version of Peter Grill's answer taking in account the suggestions I made in the comment below Peter's answer and a bit more to show some more possibilities of the pgfplots package ...

Please have a look at the comments in the code for more details.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher so you don't have to put `axis cs:'
        % in front of every TikZ (not PGFPlots) coordinate
        % (in this example the coordinates of the rectangle)
        compat=1.11,
        % declare a variable to only have one place where to change the
        % symmetric axis limits
        /tikz/declare function={
            AxisLimit=1.5;
        },
        % define a custom layer set, so the *axis labels* are drawn on top of
        % the draw/plot commands, which otherwise would be partially hidden
        % for this example
        % (I just used the default set and moved `axis tick labels' after `main')
        layers/tick labels on top/.define layer set={
            axis background,axis grid,axis ticks,axis lines,
            main,axis tick labels,axis descriptions,axis foreground
        }{/pgfplots/layers/standard},
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            % don't use a boxed axis but centered lines
            % (`center' is an alias for `middle')
            axis lines=middle,
            % apply the axis limits with the help of the defined variable
            xmin=-AxisLimit,    xmax=AxisLimit,
            ymin=-AxisLimit,    ymax=AxisLimit,
            % to have equal vector length for both axis ...
            axis equal image=true,
            % show some minor ticks
            minor tick num=4,
            % show a grid for both of the ticks (major and minor)
            grid=both,
            % apply the above created layer set
            set layers=tick labels on top,
            % in case that only isn't enough you could modify the style of the
            % tick labels to have a white background
            % (which in addition could be a bit transparent)
            tick label style={
                fill=white,
                fill opacity=0.75,
                % (but of course the text should not be transparent)
                text opacity=1,
            },
        ]
            % you could either draw a rectangle using a TikZ command ...
            \draw [
                blue,
                ultra thick,
            ] (-1,-1) rectangle (1,1);

            % ... or by using a PGFPlots command
            \addplot [
                mark=none,
                red,
                thick,
            ] coordinates {
                (-1,-1) (1,-1) (1,1) (-1,1)
            }
                % use this TikZ command to connect the last with the first
                % path element
                -- cycle;
        \end{axis}
    \end{tikzpicture}
\end{document}

image showing the result of above code

3
  • 1
    this is excellent as always. – Jan Jan 19 '17 at 17:51
  • Can you put the plus sign explicitly on the plot i.e. '+1' instead of just '1'? – Leeser Jan 19 '17 at 18:21
  • 1
    @Leeser, sure, add /pgf/number format/showpos=true, to the tick label style options. Of course this will only show the + sign for the axis tick labels. If you also need them at other places, move the key-value to the appropriate place or add it there also. – Stefan Pinnow Jan 19 '17 at 19:10
0

Here another solution using pgfmath. Just for fun in and better understanding of the unit ball. The values are calculated by the evaluate of the foreach-loop and by pgfmathparse. The int() function is used to strip the decimal values from the result.

\documentclass[tikz, border=5mm]{standalone}

\begin{document}
  \begin{tikzpicture}
    \draw [gray!50!white, step=.2] (-1.3,-1.3) grid (1.3,1.3);
    \draw [thick, ->] (0,-1.4) -- (0,1.4);
    \draw [thick, ->] (-1.4,0) -- (1.4,0);

    \draw [red, ultra thick] (-1,-1) rectangle (1,1);

    \foreach [evaluate=\a as \x using cos(\a),evaluate=\a as \y using sin(\a)] \a in {0,90,180,270} %
        \node [circle, fill, inner sep=1pt, label={-45:\pgfmathparse{int(\x ^2+\y^2)}\pgfmathresult}] at (\x, \y) {};
  \end{tikzpicture}
\end{document}

unit ball example

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.