3

I'm trying to create a rectangular graph using some variation of Tikz such that I count by pi/4 on the x-axis. I can mostly get it; however, I want the labels to be in exact form (i.e., fractions, with the pi symbol). Here's what I have so far:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \tkzInit[xmin=0,xmax=3.1415,ymin=-1.5,ymax=1.5,xstep=.785398163,ystep=.5]
    \tkzGrid
    \tkzAxeXY
\end{tikzpicture}
\end{document}

I've been relying heavily on tkz-euclide for my project, but I am willing to venture wherever I need to to get the job done. I tried using GeoGebra's Export to Tikz, but the results were much different than what I have above, and I had trouble editing it down to the point where I was happy with it. I have several of these kinds of graphs to make, so I'm hoping that I can have an easily altered code base to build on.

Thanks for your help!

1 Answers1

2

I would recommend moving to regular tikz and pgfplots because they are way more flexible and also allow to tweak (for example) the background of tick labels. In both solutions presented here the some grid lines are penetrating the tick label space. If you are willing to switch, you could use the answer already recommended in the comment below your question.

If you want to stick to tkz-euiclide, you could use one of the two following solutions.

First Solution

This solution is cleaner than the second solution because only the part on the coordinate system that requires special treatment is changed. However this solution is more complicated and also has its issues.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \tkzInit[xmin=0,xmax=1,ymin=-1.5,ymax=1.5,xstep=.25,ystep=.5]
    \tkzGrid
    \tkzAxeY
    \tkzDrawX
    \tkzClip[space=0.1]
    \begin{scope}[xscale=4/pi]
        \tkzLabelX[trig=4,below=.25cm]
    \end{scope}
\end{tikzpicture}
\end{document}

You can split \tkzAxeXY into its underlying parts \tkzAxeX and \tkzAxeY. The y-axis is not a problem, therefore we will concentrate on the x axis only. \tkzAxeX can be split into \tkzDrawX and \tkzLabelX, which is necessary because the axis line and ticks must be drawn separately from the axis ticks labels for this solution to work.

By splitting all this up we can draw a dummy coordinate system with xmax=1 and xstep=0.25, which will yield correct lines for the grid and the axis ticks, and utilize the trig=4 option only for the tick labels, which will yield correct labels at wrong positions. To correct the positions a scope with xscale=4/pi from normal tikz is used. (Notice that I used pi here instead of a number, this is a special constant that tikz and also tkz-euclide know.)

Unfortunately the internals of tkz-euclide cause a rounding error that make \tkzLabelX print one tick label to much. This excessive tick label is clipped away with \tkzClip.

Side note: If you want to draw points or other things into your coordinate system, remember that pi is located at 1.

enter image description here

Second Solution

I only stumbled upon this solution on my way to the first solution and I actually have no idea why exactly it works. I will still post it, maybe someone else knows how the parameters xmax and xstep in \tkzInit play together to allow this output.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tkz-euclide}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[xscale=4/pi]
    \tkzInit[xmin=0,xmax=pi,ymin=-1.5,ymax=1.5,xstep=.999,ystep=.5]
    \tkzGrid[xstep=0.25*pi]
    \tkzAxeY
    \tkzAxeX[trig=4,below=.25cm]
\end{tikzpicture}
\end{document}

In this case the whole picture has to be scaled in x direction, which also move the x axis label further to the right. This is why I personally consider this solution less clean than the first solution.

Side note: If you want to draw points or other things in this coordinate system pi is located at pi, which is nice.

enter image description here

flammermann
  • 307
  • 7
  • I used your first solution; thank you so much! Are there other examples like this that I could peruse? I will need to make several graphs (some on the polar plane) and I would like to be prepared. I could not find examples like this in the documentation for tkz-euclide or tikz (including the gigantic 1,000 page manual for tikz). – josephgerth Jul 15 '20 at 00:04
  • Since I heard about tkz-euclide for the first time yesterday, I am not really prepared to provide examples for certain situations out of the box, simply because I have no extensive knowledge about that package and it takes me quite long to build an answer. However, I like challenges and I would be happy to provide more answers to specific questions. Just like you did here you could present a starting point, set a goal and I would answer if I find the time (or some other community member). I will watch the tag tkz-euclide to see those questions more quickly. – flammermann Jul 15 '20 at 07:48
  • If the answer solves your problem, please also mark it as accepted answer, this makes it easier for other members and visitors to see that a questions is solved. – flammermann Jul 15 '20 at 07:49