Graphics, Figures & TablesFractional Tick Marks on Number Lines

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

Fractional Tick Marks on Number Lines

Post by LaTexLearner »

I've recently finished moving and am now back to learning LaTeX and TikZ!

I want to create a number line from 0 to 3 with tick marks at every, say, 1/2 or 1/3 or whatever.

I have figured out how to make the whole number part of the number line, but can't figure out the fractional tick marks. How do I improve on the following?

CODE THAT DID NOT WORK

Code: Select all

\documentclass[border=10pt]{article}

\usepackage{tikz}

\begin{document}

%THIS PICTURE OF THIRDS DOES NOT WORK.
%Can you not use fractions in the "foreach" command? 
%If not, do I have to approximate with decimals (see below in post)
\begin{tikzpicture}[scale=2]
  \draw[-latex] (0,0) -- (4.5,0); % the x-axis
  \foreach \x in  {0/3,1/3,...,12/3} % tick marks for every 1/3
    \draw (\x,4pt) -- (\x,-4pt);
  \foreach \x in {0,...,4} % the whole numbers labels
    \node [below] at (\x,-0.2) {$\x$};
\end{tikzpicture}

\end{document}

CODE THAT WORKED IN TERMS OF A ROUGH PICTURE, BUT SEEMS NON-ELEGANT AND CONTAINS ROUNDING ERRORS

Code: Select all

\documentclass[border=10pt]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=2]
  \draw[-latex] (0,0) -- (4.5,0); % the x-axis
  \foreach \x in  {0,0.33,0.67,1,1.33,1.67,2,2.33,2.67,3,3.33,3.67,4} % tick marks for every approx. 1/3
    \draw (\x,4pt) -- (\x,-4pt);
  \foreach \x in {0,...,4} % the whole numbers labels
    \node [below] at (\x,-0.2) {$\x$};
\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

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Fractional Tick Marks on Number Lines

Post by rais »

LaTexLearner wrote: I have figured out how to make the whole number part of the number line, but can't figure out the fractional tick marks. How do I improve on the following?
Isn't the /-character used by the \foreach command as separator between different values (for the same run)?

You could try sth. like

Code: Select all

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=2]
  \draw[-latex] (0,0) -- (4.5,0); % the x-axis
  \foreach \x in  {0,1,...,12} % tick marks for every 1/3
    \draw (\x/3,2pt) -- +(0,-4pt);
  \foreach \x in {0,...,4} % the whole numbers labels
    \draw (\x,4pt) -- +(0,-8pt) node [below] {$\x$};
\end{tikzpicture}

\end{document}
KR
Rainer
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Fractional Tick Marks on Number Lines

Post by LaTexLearner »

rais wrote: Isn't the /-character used by the \foreach command as separator between different values (for the same run)?
I'm not sure what you mean by this but...
rais wrote:
LaTexLearner wrote: I have figured out how to make the whole number part of the number line, but can't figure out the fractional tick marks. How do I improve on the following?
Isn't the /-character used by the \foreach command as separator between different values (for the same run)?

You could try sth. like

Code: Select all

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[scale=2]
  \draw[-latex] (0,0) -- (4.5,0); % the x-axis
  \foreach \x in  {0,1,...,12} % tick marks for every 1/3
    \draw (\x/3,2pt) -- +(0,-4pt);
  \foreach \x in {0,...,4} % the whole numbers labels
    \draw (\x,4pt) -- +(0,-8pt) node [below] {$\x$};
\end{tikzpicture}

\end{document}
KR
Rainer
... I completely understand this code. Thank you!

So, fractions CAN go in the \draw command but not in the \foreach command. Is that right?
Last edited by LaTexLearner on Sat Jul 18, 2015 10:00 pm, edited 1 time in total.
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Fractional Tick Marks on Number Lines

Post by rais »

LaTexLearner wrote:
rais wrote: Isn't the /-character used by the \foreach command as separator between different values (for the same run)?
I'm not sure what you mean by this but...
well, consider this code snippet:

Code: Select all

\foreach \x/\y in {1/2, 3/4, 5/6}
now there's two variables---\x and \y---to be considered for the \foreach command...
LaTexLearner wrote: So, fractions CAN go in the \draw command but not in the \foreach command. Is that right?
Not exactly. You could `hide' the /-character from the \foreach command by surrounding each fraction by an additional pair of braces, such as

Code: Select all

\foreach \x in {{1/3},{2/3},{3/3}}
KR
Rainer
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Fractional Tick Marks on Number Lines

Post by LaTexLearner »

rais wrote:
LaTexLearner wrote: So, fractions CAN go in the \draw command but not in the \foreach command. Is that right?
Not exactly. You could `hide' the /-character from the \foreach command by surrounding each fraction by an additional pair of braces, such as

Code: Select all

\foreach \x in {{1/3},{2/3},{3/3}}
KR
Rainer
Problem semi-fixed. Two issues remain for me.

First, it seems that when I add those extra braces, it loses the "math layout" that I want, i.e. I want the fractions on top of the number line to have a horizontal fraction bar.

Also, it seems unable to see that I am counting by halves in the \foreach command

Code: Select all

\documentclass[12pt,letterpaper]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[scale=3]

% x-Axis
\draw[-latex] (0,0)--(4.5,0);

% Whole number labels below
\foreach \x in {0,1,2,3,4}
  \node [below] at (\x,-.15) {$\x$};

% DID NOT WORK IN GETTING FRACTION NUMBERS ABOVE AXIS
% COULD NOT SEE COUNTING BY HALVES?
% \foreach \x in { {0/2},{1/2},{2/2},{3/2},...,{8/2}  }
%   \node [above] at (\x,0.15) {$\x$};

% DID WORK IN GETTING FRACTION NUMBERS ABOVE AXIS
% BUT HAD TO WRITE OUT ALL THE HALVES
% AND DID NOT GET FRACTION WITH HORIZONTAL FRACTION BAR
\foreach \x in { {0/2},{1/2},{2/2},{3/2},{4/2},{5/2},{6/2},{7/2},{8/2}  }
  \node [above] at (\x,0.15) {$\x$};


\end{tikzpicture}

\end{document}
Last edited by LaTexLearner on Sat Jul 18, 2015 10:48 pm, edited 1 time in total.
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Re: Fractional Tick Marks on Number Lines

Post by LaTexLearner »

Another side comment: Why does the tikz picture indent?

Since I know I won't need that, should I write a macro to turn that off?
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Fractional Tick Marks on Number Lines

Post by rais »

LaTexLearner wrote: First, it seems that when I add those extra braces, it loses the "math layout" that I want, i.e. I want the fractions on top of the number line to have a horizontal fraction bar.
Well, if you don't want to rip your \x apart to split it into denominator and numerator, use two variables.
LaTexLearner wrote: Also, it seems unable to see that I am counting by halves in the \foreach command
Yes, the additional braces seem to hide more from \foreach than they were supposed to.
Back to the original approach, you'd just need a second variable---say, \y---for the denominator.
Since it would be constant (2 for halves), it doesn't need to show up in the \foreach statement.
LaTexLearner wrote: Another side comment: Why does the tikz picture indent?
If it's not the normal first-line-of-a-paragraph-indent that you mean (where you could put a \noindent directly in front of the {tikzpicture} environment): you put a node at x=0. Each node is surrounded by space; you may want to look up `inner sep' in the tikz manual. Here, I just made it visible with the fill option:

Code: Select all

\documentclass[12pt,letterpaper]{article}

\usepackage{tikz}
\usetikzlibrary{arrows}

\begin{document}

\begin{tikzpicture}[scale=3]

% x-Axis
\draw[-latex] (0,0)--(4.5,0);

% Whole number labels below
\foreach \x in {0,1,2,3,4}
  \node [below, fill=cyan] at (\x,-.15) {$\x$};

\def\y{2}% the denominator
\foreach \x in {0,1,...,8}% and here just the numerator
  \node [above] at (\x/\y,0.15) {$\frac{\x}{\y}$};

\end{tikzpicture}

\end{document}
KR
Rainer
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Fractional Tick Marks on Number Lines

Post by LaTexLearner »

Got it. Thanks!

Next question. :)

I am trying to put in answer spaces above the number line. Why did the first code fail why the second code worked? Can there be only one \draw command under a \foreach command?

CODE THAT FAILED

Code: Select all

\begin{tikzpicture}[scale=3]

% x-axis
\draw (0,0)--(4,0);

%Make x-axis into rectangle that can be shaded
\draw (0,0.1)--(4,0.1);

% vertical tick marks for fifths
\foreach \x in {0,...,20}
   \draw (\x/5,-0.1)--(\x/5,0.1);

\foreach \x in {0,...,4}
   \node [below] at (\x,-0.1) {\x};
   
% ANSWER SPACES THAT DID NOT WORK
\foreach \x in {0,...,20}
  \draw[thin, gray] (\x/5-0.09,0.2)--(\x/5-0.09,0.45)--(\x/5+0.09,0.45)--(\x/5+0.09,0.2)--cycle;
  \draw[thin, gray, -latex] (\x/5,0.2)--(\x/5,0.12);
   
\end{tikzpicture}
CODE THAT WORKED

Code: Select all

\begin{tikzpicture}[scale=3]

% x-axis
\draw (0,0)--(4,0);

%Make x-axis into rectangle that can be shaded
\draw (0,0.1)--(4,0.1);

% vertical tick marks for fifths
\foreach \x in {0,...,20}
   \draw (\x/5,-0.1)--(\x/5,0.1);

\foreach \x in {0,...,4}
   \node [below] at (\x,-0.1) {\x};
   % ANSWER SPACES THAT DID WORK
\foreach \x in {0,...,20}
  \draw[thin, gray] (\x/5-0.09,0.2)--(\x/5-0.09,0.45)--(\x/5+0.09,0.45)--(\x/5+0.09,0.2)--cycle;

\foreach \x in {0,...,20}
   \draw[thin, gray, -latex] (\x/5,0.2)--(\x/5,0.12);
   
\end{tikzpicture}
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Fractional Tick Marks on Number Lines

Post by rais »

LaTexLearner wrote: I am trying to put in answer spaces above the number line. Why did the first code fail why the second code worked? Can there be only one \draw command under a \foreach command?
pgfmanual wrote: The general syntax is \foreach <variable> in {<list of values>} <commands>. Inside the <commands>, the <variable> will be assigned to the different values. If the <commands> do not start with a brace, everything up to the next semicolon is used as <commands>.
So in your case,

Code: Select all

\foreach \x in {0,...,20}{%<--
  \draw[thin, gray] (\x/5-0.09,0.2)--(\x/5-0.09,0.45)--(\x/5+0.09,0.45)--(\x/5+0.09,0.2)--cycle;
  \draw[thin, gray, -latex] (\x/5,0.2)--(\x/5,0.12);
}%<--
should do.

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

Fractional Tick Marks on Number Lines

Post by LaTexLearner »

Got it. Thank you!

I need to start learning all the "defaults" of LaTeX and TikZ, i.e. what the code assumes I want if I don't type anything...
Post Reply