Graphics, Figures & TablesMake sense of number line code

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Make sense of number line code

Post by LaTexLearner »

I am now kind of able to use this code, but haven't quite figured why it works.

I put my questions inside the code.

Code: Select all


\documentclass[letterpaper]{article}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}

\draw[-latex][thick] (0,0) -- (5.5,0);
%How do I make number line longer, say, to go from (0,0) to (5.5,0) but make the unit length, say, 4cm and have all my tick marks and numbers "come along"?


\foreach \x in {0,1,...,5}
\draw[shift={(\x,0)},color=black] (0pt,4pt) -- (0pt,-4pt);
% This is where the vertical tick marks go... 
% but what is the "shift" part for in the optional argument?

\foreach \x in {0,1,...,5}
\draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-6pt) node[below] 
{$\x$};
% This is how you put numbers under the tick marks...
% But what is the shift part here for? And what is the node part for?

\end{tikzpicture}


\end{document}

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

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Make sense of number line code

Post by Johannes_B »

The foreach-loop loops over all elements of a list and does stuff to the next token, which in your example is the draw command. It draws the tick-line and then shifts that line according to the current list value.

The very same is true for the other loop, once again drawing something and shifting (or rather shifting, then drawing), but after drawing, it typesets the contents of the node below the current position. The content of the node is the current value in the foreach list.

If no explicit unit is given, you cn control it as seen in the optional argument of the first tikzpicture environment. Look closely at the colors in the first line, do you see anything that could be improved?

Code: Select all

\documentclass[letterpaper]{article}

\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[x=2cm,y=.25cm]

	\draw[-latex][thick] (0,0) -- (5.5,0);

	\foreach \x in {0,1,...,5}
	\draw[shift={(\x,0)},color=black] (0pt,0) -- (0pt,-6) node[below]
	{$\x$};

	\foreach \x in {0,1,...,5}
	\draw[shift={(\x,0)},color=red] (0pt,4pt) -- (0pt,-4pt);

\end{tikzpicture}

\begin{tikzpicture}
	\foreach \x in {0,1,...,10}
	\draw[shift={(\x,0)},color=blue] (0pt,4pt) -- (0pt,-4pt) node [below]
	{$\x$};
\end{tikzpicture}
\end{document}

The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply