2

I would like to create a memory card game for my pupils and generate quadratic cards of 6cm width and height which are then embedded in a master file which puts the cards into a grid.

A single card could look like this:

\documentclass{article}
\usepackage{pgfplots}
\usepackage[T1]{fontenc} % Input format
\usepackage[paperwidth=6cm,paperheight=6cm,top=0cm,bottom=0cm,left=0cm,right=0cm]{geometry}

\begin{document}
    \thispagestyle{plain}
    \pagenumbering{gobble}
    \begin{center}
        \vspace*{\fill}
        \resizebox{0.9\linewidth}{!}{%
            \begin{tikzpicture}[>=stealth]
                \begin{axis}[
                    axis equal,
                    xtick distance=5,
                    ytick distance=5,
                    xmin=-11,xmax=11,
                    ymin=-11,ymax=11,
                    grid=both,
                    axis lines=middle,
                    minor tick num=4,
                    enlargelimits={abs=0.5},
                    axis line style={->},
                    ticklabel style={font=\tiny,fill=white},
                    xlabel={$x$},
                    ylabel={$y$},
                    ]
                    \addplot[no marks,blue,-] expression[domain=-10:10,samples=100]{2*x+3};
                \end{axis}
            \end{tikzpicture}
        }
        \vspace*{\fill}
    \end{center}
\end{document}

As the card is quadratic and the x and y range are of the same length (-11 to 11 each), I'd expect the plot to be quadratic as well, but it is not: The x axis is made longer than the y axis for some reason, so the top and bottom borders are bigger than the left and right border. Also the plot is not horizontally centered although I'm using the center environment. Here is how it looks like:

The output of the code example above

Hints on what I am doing wrong are greatly appreciated!

5
  • 1
    It seems you are graphing 2x+3, so why would you expect the plot to be quadratic (and not linear)?
    – Dan
    Nov 14, 2021 at 17:59
  • Only part answer, hence comment: axis equal doesn't set the size of the axis box. Remove the \resizebox and add scale only axis, width=0.9\linewidth, height=0.9\linewidth, to the axis options. Nov 14, 2021 at 19:08
  • @Dan: I guess, my choice of words was quite poor, I referred to the looks of the coordinate system, not the function type.
    – Photon
    Nov 15, 2021 at 12:15
  • @TorbjørnT.: Thanks, I went with copy&pasting Dan's answer for now but I will have a more detailed look at both of your proposals to better understand how it works as soon as time permits!
    – Photon
    Nov 15, 2021 at 12:17
  • The TeX.SE website it is a great community instead of PSE :-))))+1.
    – Sebastiano
    Nov 29, 2021 at 16:45

1 Answer 1

3

To make the axes actually equal we need to use axis equal image instead of axis equal, and add a \nointerlineskip before \vspace to properly center vertically. Removing some spurious spaces at the end of lines fixes the horizontal alignment, and changing the domain and range to [-12,12] instead of [-11,11] avoids a rounding error within pgfplots from not drawing the final y grid line (which occurs when we switch to axis equal image).

\documentclass{article}
\usepackage{pgfplots}
\usepackage[T1]{fontenc} % Input format
\usepackage[paperwidth=6cm,paperheight=6cm,top=0cm,bottom=0cm,left=0cm,right=0cm]{geometry}

\begin{document}
    \thispagestyle{plain}
    \pagenumbering{gobble}
    \begin{center}
        \nointerlineskip%<--
        \vspace*{\fill}
        \resizebox{0.9\linewidth}{!}{%
            \begin{tikzpicture}[>=stealth]
                \begin{axis}[
                    axis equal image,%<--
                    xtick distance=5,
                    ytick distance=5,
                    xmin=-12,xmax=12,%<--
                    ymin=-12,ymax=12,%<--
                    grid=both,
                    axis lines=middle,
                    minor tick num=4,
                    enlargelimits={abs=0.5},
                    axis line style={->},
                    ticklabel style={font=\tiny,fill=white},
                    xlabel={$x$},
                    ylabel={$y$},
                    ]
                    \addplot[no marks,blue,-] expression[domain=-12:12,samples=100]{x^2-5*x};
                \end{axis}
            \end{tikzpicture}%<--
        }%<--
        \vspace*{\fill}
    \end{center}
\end{document}

enter image description here

2
  • Thanks a lot! It's mysterious, I cannot find any spaces after any of the code lines but for some reason my code messes up the horizontal centering even if I make it looks like yours identically, while if I copy&paste yours it works. But I'm quite in a hurry lately so I guess, I will have another look into it at a later point.
    – Photon
    Nov 15, 2021 at 12:19
  • @Photon That is strange. Make sure you have a % at the direct end of the \end{tikzpicture} and the following line as well as the \resizebox{0.9\linewidth}{!}{ line.
    – Dan
    Nov 15, 2021 at 17:28

You must log in to answer this question.

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