5

I was wondering if there was a more succinct way to express the following plot (I'm using the plain format):

\input tikz
\tikzpicture
  \draw[help lines, ystep=.5] (-.2,-10) grid (12.5,15);
  \draw[->] (0, -10) to (0, 15) node[above] {$y$};
  \draw[->] (0, 0) to (12.5, 0) node[right] {$x$};
  \draw (1,-.5) node {$1$};
  \draw (2,-.5) node {$2$};
  \draw (3,-.5) node {$3$};
  \draw (4,-.5) node {$4$};
  \draw (5,-.5) node {$5$};
  \draw (6,-.5) node {$6$};
  \draw (7,-.5) node {$7$};
  \draw (8,-.5) node {$8$};
  \draw (9,-.5) node {$9$};
  \draw (10,-.5) node {$10$};
  \draw (11,-.5) node {$11$};
  \draw (12,-.5) node {$12$};

  \draw (-.5,-10) node {$-20$};
  \draw (-.5,-9) node {$-18$};
  \draw (-.5,-8) node {$-16$};
  \draw (-.5,-7) node {$-14$};
  \draw (-.5,-6) node {$-12$};
  \draw (-.5,-5) node {$-10$};
  \draw (-.5,-4) node {$-8$};
  \draw (-.5,-3) node {$-6$};
  \draw (-.5,-2) node {$-4$};
  \draw (-.5,-1) node {$-2$};
  \draw (-.5,0) node {$0$};
  \draw (-.5,1) node {$2$};
  \draw (-.5,2) node {$4$};
  \draw (-.5,3) node {$6$};
  \draw (-.5,4) node {$8$};
  \draw (-.5,5) node {$10$};

  \draw (0,-10) circle (2pt);
  \draw (1,-9) circle (2pt);
  \draw (2,-8) circle (2pt);
  \draw (3,-7) circle (2pt);
  \draw (4,-6) circle (2pt);
  \draw (5,-5) circle (2pt);
  \draw (6,-4) circle (2pt);
  \draw (7,-3) circle (2pt);
  \draw (8,-2) circle (2pt);
  \draw (9,-1) circle (2pt);
  \draw (10,0) circle (2pt);
  \draw (11,1) circle (2pt);
  \draw (12,2) circle (2pt);
\endtikzpicture
\bye

It comes out so big it doesn't fit on a page.

  • @marmot well I suppose I could try and derive a plain version from a LaTeX answer. – morbusg Mar 20 at 0:54
6

Here's a LaTeX version.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \draw[help lines, ystep=.5] (-.2,-10) grid (12.5,15);
  \draw[->] (0, -10) to (0, 15) node[above] {$y$};
  \draw[->] (0, 0) to (12.5, 0) node[right] {$x$};
  \foreach \X in {0,...,12}
  {\ifnum\X>0
  \node at (\X,-0.5) {$\X$};
  \fi
  \draw (\X,-10+\X) circle (2pt); }
  \foreach \Y in {-20,-18,...,10}
  {\node at (-0.5,\Y/2) {$\Y$};}
\end{tikzpicture}
\end{document}

Amazingly the following does run through with pdftex

\input tikz
\tikzpicture
\draw[help lines, ystep=.5] (-.2,-10) grid (12.5,15);
  \draw[->] (0, -10) to (0, 15) node[above] {$y$};
  \draw[->] (0, 0) to (12.5, 0) node[right] {$x$};
  \foreach \X in {0,...,12}
  {\ifnum\X>0
  \node at (\X,-0.5) {$\X$};
  \fi
  \draw (\X,-10+\X) circle (2pt); }
  \foreach \Y in {-20,-18,...,10}
  {\node at (-0.5,\Y/2) {$\Y$};}
\endtikzpicture
\bye
  • Thanks! I don't know why, but for some reason the \foreach wasn't working for me earlier – that shortens the code nicely. I wonder if the new datavisualization command could somehow be used here. – morbusg Mar 20 at 1:04
  • @morbusg Yes, it could. The question is what you want to achieve. If you only want some axis, grid, and plot of that sort you may be better off with pgfplots. However, I have no experience with plain TeX. – marmot Mar 20 at 1:07
  • 2
    Well the part inside \begin{tikzpicture} … \end{tikzpicture} in your first code block uses only TikZ commands so it's not so surprising that it works with plain TeX too (because TikZ has been designed that way), though I guess it's amazing that TikZ has been implemented that way in the first place. :-) I expect most “LaTeX” answers using only TikZ commands can be quite easily converted to run with pdftex. – ShreevatsaR Mar 20 at 1:59
  • 2
    @ShreevatsaR Yes, that's all true, but the above is the second plain TeX document I ever compiled so for me it was a miracle that it immediately worked. – marmot Mar 20 at 2:01
  • @marmot Ah I know the feeling :) You're right – ShreevatsaR Mar 20 at 2:40
4

So after spending a night reading the manual while in a trial-error-loop, after many solemn utterances to invoke supernatural power to inflict harm or punishment on something, all of which now regretted, I finally – victoriously – formed the following:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{datavisualization, datavisualization.formats.functions}
\begin{document}
\begin{tikzpicture}
  \datavisualization[
    school book axes
    , x axis={unit length=5mm}
    , y axis={
      unit length=5mm
      , ticks={step=2}
      , grid
      , scaling = min at 0cm and max at 5cm
    }
    , visualize as line
  ]
  data[format=function] {
    var x : interval [0:12];
    func y = (\value x - 10) * 2;
  };
\end{tikzpicture}
\end{document}

Boom! Pow! Shazam! Holy function data format, Batman! This is pretty darn nice, it has to be said.

Only trouble I found, is that it does not work with the plain format, forcing the user to descend to a lower plane of enlightenment.

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.