Graphics, Figures & TablesNumber Lines with TikZ

Information and discussion about graphics, figures & tables in LaTeX documents.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Number Lines with TikZ

Post by rais »

LaTexLearner wrote:I am, though, a little worried that it will take me hundreds of hours to get even the basics down and it would therefore be faster to use another program.
The way I see it: you may need those hundreds of hours to understand *everything* about tikz. But do you really need it?
AFAICS, there's basically just straight lines, some rectangles perhaps, and text involved.
I fiddled around with Stefan's code a bit:

Code: Select all

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
% (rais) the following two commands may not seem necessary; they're just intended to throw a warning in case some other package already defined those macros
\newcommand*\fractionprintnum{}%
\newcommand*\fracpart{}%
\newcommand*\fracyplace{0}% (rais) the vertical offset for the fractions,e.g., 0 for the first bar, -2 for the second bar, and so on
\def\fracpart#1/#2/#3\null{% (rais) similar to \fractionprintnum
% one could catch here, if the mandatory argument to \fractionprint didn't contain a / character at all (arg #2 would be empty)
  \frac{#1}{#2}%
}
\def\fractionprintnum#1+#2+#3\null{% (rais) the main goal of this macro is: split the integer part off the fraction part.
% This macro is not intended to be called directly, that's why I'd rather put at least one @ character in its name. OTOH, it's just an example,how things could be done...
% If \fractionprint (see below) gets called with, say, {a+b/c}, the `a' will be put in arg #1 and `b/c' will end up in arg #2 (and arg #3 collects the last + character).
% If \fractionprint gets called with {a/b}, there won't be an integer part. Arg #1 will contain the a/b, arg #2 will be empty, and arg #3 should be empty, too. Then again, arg #3 just plays the role of a garbage collector...
  \ifx\relax#2\relax % if there's no integer part
    \ensuremath{\fracpart#1//\null}% then the fraction part (if any) will be in arg #1
  \else
    \ensuremath{#1\fracpart#2//\null}% else output the integer part (#1) and the fraction part will be in arg #2
  \fi
}
% The next macro calculates the first argument and uses that position
% to print the next argument
% (rais) since the next argument could be assembled from the first, I skipped the second mandatory argument, see \fractionprintnum above---it makes calling the \fractionprint command a bit easier.
% And then I thought it might be neat to be able to place the fraction below the bar, as well as above...hence there's now an optional argument, too
\newcommand*{\fractionprint}[2][above=0.3cm]{%
  \pgfmathparse{3*(#2)}% (rais) jut a bit more space
  \draw (\pgfmathresult,\fracyplace+0.2) -- +(0,-0.3)% (rais) tick added and used \fracyplace as y offset
    node[blue, #1] {\fractionprintnum#2++\null}% (rais) blame it on my (rather limited) experience with turbo pascal, that I've eliminated the final ; character here ;-)
}
\begin{document}
\begin{tikzpicture}
  \filldraw[fill=black!30] (0,0) rectangle (7.5,0.2);% (rais) the gray bar inside. The width (7.5) was calculated as 3 * 5/2.
% The factor of 3 I included, in case your students have to copy everything to paper, first: for the width of 1/3, they can use 1 cm.
  \draw[black!20] (7.5, 0) -- +(0,-2);% (rais) just a help line
  \draw[semithick] (0,0) rectangle (9,0.2);% (rais) the x-axis as rectangle
  \foreach \x in  {0,...,3} % tick marks
    \draw (3*\x,0.2) -- +(0,-0.3) node[below=0.2cm]{$\x$};
  \foreach \x in {0,...,6}% (rais) fractions above
    \fractionprint{\x/2};
  \fractionprint[below]{1+1/2};
  \fractionprint[below, red]{2+1/2};
  \filldraw[fill=black!30] (0,-2) rectangle +(7.5,0.2);% (rais) 3 * 10/4.
  \draw[semithick] (0,-2) rectangle +(9,0.2);% (rais) the second bar
  \foreach \x in  {0,...,3} % tick marks
    \draw (3*\x,-1.8) -- +(0,-0.3) node[below=0.2cm]{$\x$};
  \renewcommand*\fracyplace{-2}% the y offset for the fractions
  \foreach \x in {0,...,12}% (rais) quarters
    \fractionprint{\x/4};
  \foreach \x in {1,2}
    \foreach \xb in {1,2,3}
      \fractionprint[below]{\x+\xb/4};
% (rais) the following part just demonstrates, how those funny arrows could be drawn. Come to think of it, an adapted version may have found its way inside markings of the tick marks above
  \foreach \x/\y in {0/0, 1/0, 2/0, 3/0, 0/-2, 1/-2, 2/-2, 3/-2}
    \draw[very thin] (3*\x, \y-0.15) -- ++(0.1,-0.1) -- ++(-0.05,0)
      -- ++(0,-0.1) -- ++(0.15,0) -- ++(0,-0.4) -- ++(-0.4,0)
      -- ++(0,0.4) -- ++(0.15,0) -- ++(0,0.1) -- ++(-0.05,0) -- cycle;
\end{tikzpicture}
\end{document}
And trust me when I say, having no previous programming experience is---thinking of LaTeX---rather a blessing, than a curse.

(I did have contact with pascal, C, and a few others, before trying my hand at LaTeX...TeX's what? Stomach?? (see the TeXbook or perhaps TeXbyTopic)

KR
Rainer

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Re: Number Lines with TikZ

Post by LaTexLearner »

Wow, that looks fantastic!

I'm going to take a closer look at the code tonight.

Thanks!
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Number Lines with TikZ

Post by Johannes_B »

Stefan_K wrote:Large manuals a great. The more explained, the better. The more examples, the better. The more powerful package, the better. It's hardly understandable to reject a long manual just because of the size. I guess you did not mean it that way.

Stefan
Contra. Quoting the TeX.SX chat room
Every time i have to look up the simplest commands to draw something, for example a circle. To get that information i have to open a 1200 page long document. This is a bit ridiculous i think. They should split the doc, maybe the whole introduction, the main function, the libraries. I cannot be the only one having this thought, right?
Reference

That is not concerning powerfulness or the amount of examples, that part is great. It is the fact, that searching for a special term, my pdf reader suddenly uses up to 1 GB of RAM (out of 2 GB) slowing down my machine completely. Well, i could upgrade my system or use another pdf-viewer, but ...
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10330
Joined: Mon Mar 10, 2008 9:44 pm

Re: Number Lines with TikZ

Post by Stefan Kottwitz »

Re. The point was in my opinion the amount, not if it's splitted or not.
Though I agree it would be very good to split the manual in tutorial part, basic reference, library reference etc.

Stefan
LaTeX.org admin
Post Reply