6

In the following code, how can I display the result of a style-defined number multiplication, to show 50 instead of 25*2, and so on.

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=1., transform shape,linecolor/.style={thick, blue}]
\pgfmathsetmacro{\incrmnt}{1.}
\pgfmathsetmacro{\inc}{25}
\draw [thick,-latex](0,0) -- (8,0);
\draw [thick,-latex](0,0) -- (0,6);
\draw [linecolor] (0,\incrmnt*1) +(180:.15cm) -- +(0:.15cm) node [xshift=-.68cm, yshift=0cm] {2};
\draw [linecolor] (0,\incrmnt*2) +(180:.15cm) -- +(0:.15cm) node [xshift=-.68cm, yshift=0cm] {4};
\draw [linecolor] (0,\incrmnt*3) +(180:.15cm) -- +(0:.15cm) node [xshift=-.68cm, yshift=0cm] {6};
\draw [linecolor] (0,\incrmnt*4) +(180:.15cm) -- +(0:.15cm) node [xshift=-.68cm, yshift=0cm] {8};
\draw [linecolor] (0,\incrmnt*5) +(180:.15cm) -- +(0:.15cm) node [xshift=-.68cm, yshift=0cm] {10};

\draw [linecolor] (\incrmnt*1,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*1};
\draw [linecolor] (\incrmnt*2,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*2};
\draw [linecolor] (\incrmnt*3,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*3};
\draw [linecolor] (\incrmnt*4,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*4};
\draw [linecolor] (\incrmnt*5,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*5};
\draw [linecolor] (\incrmnt*6,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*6};
\draw [linecolor] (\incrmnt*7,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*7};
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

  • Try \pgfmathsetmacro\number{\inc*\x} and then \pgfmathprintnumber\number in a foreach loop (for example: \pgfmathsetmacro\test{2*5} and then \pgfmathprintnumber\test) … – current_user Oct 30 '18 at 13:46
  • @current_user Thank you for your comment. Would you please tell me how to use it. I could not apply your idea. I did not use foreach loop in my question. Just apply it in one of my lines – Hany Oct 30 '18 at 13:59
5

To place labels, there are left, rigth, below, above keys that place the text on the left, right, below and above the last plotted point. Thus, it is enough to draw the point where you want to place a label last and not to draw it first which complicates the placement of labels.

For the y-axis, write simply:

\draw [linecolor] (.15,\incrmnt*1) -- +(0:-.3) node [left] {2};

instead of:

\draw [linecolor] (0,\incrmnt*1) +(180:.15cm) -- +(0:.15cm) node [xshift=-.68cm, yshift=0cm] {2};

Similarly for the abscissa axis, simply write:

\draw [linecolor] (\incrmnt*1,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below] {\inc*1};

instead of

\draw [linecolor] (\incrmnt*1,0) +(90:\incrmnt*1) -- +(-90:.25cm) node [xshift=0cm, yshift=-.35cm] {\inc*1};

You will get this :

\draw [linecolor] (.15,\incrmnt*1) -- +(0:-.3) node [left] {2};
\draw [linecolor] (.15,\incrmnt*2) -- +(0:-.3) node [left] {4};
\draw [linecolor] (.15,\incrmnt*3) -- +(0:-.3) node [left] {6};
\draw [linecolor] (.15,\incrmnt*4) -- +(0:-.3) node [left] {8};
\draw [linecolor] (.15,\incrmnt*5) -- +(0:-.3) node [left] {10};
\draw [linecolor] (\incrmnt*1,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below] {\inc*1};
\draw [linecolor] (\incrmnt*2,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below]{\inc*2};
\draw [linecolor] (\incrmnt*3,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below] {\inc*3};
\draw [linecolor] (\incrmnt*4,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below] {\inc*4};
\draw [linecolor] (\incrmnt*5,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below] {\inc*5};
\draw [linecolor] (\incrmnt*6,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below] {\inc*6};
\draw [linecolor] (\incrmnt*7,\incrmnt*1) -- +(-90:\incrmnt+.25) node [below]{\inc*7};

This significantly reduces the code and makes it easier to understand.

Now you notice that your code is repeating itself, so it is easier to use two foreach loops. The syntax of foreach loops allows calculations to be performed according to a particular syntax starting with [evaluate=...]

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[scale=1., transform shape,linecolor/.style={thick, blue}]
\pgfmathsetmacro{\incrmnt}{1.}
\pgfmathsetmacro{\inc}{25}
\draw [thick,-latex](0,0) -- (8,0);
\draw [thick,-latex](0,0) -- (0,6);
\foreach \y [evaluate=\y as \aff using int(\y*2)]in {1,...,5}{
\draw [linecolor] (.15,\incrmnt*\y) -- +(0:-.3) node [left] {\aff};
}

\foreach \x [evaluate= \x as \aff using int(\inc*\x)]in {1,...,7}{
\draw [linecolor] (\incrmnt*\x,.15) -- +(-90:.3) node [below] {\aff};
}
\end{tikzpicture}
\end{frame}
\end{document}

graphs

  • Thank you for your answer and explanation. But this is an MWE, not the whole drawing. I want to concentrate only in displaying the result of a style-defined number multiplication, to show 50 instead of 25*2, and so on. Please ignore using \foreach, because the lines are irrelevant, they do not have equal length. – Hany Oct 30 '18 at 7:09
  • @Hany I have already explained here that your way of constructing graphs is too complicated. You must first build the two axes and then place its content. Please consider here that I have drawn the two axes. tex.stackexchange.com/a/457160/138900 – AndréC Oct 30 '18 at 7:16
  • Thank you for your comment. Please ignore my graph, concentrate only in displaying the result of a style-defined number multiplication, to show 50 instead of 25*2, and so on. – Hany Oct 30 '18 at 7:16
  • @Hany If your lines do not have equal length, just draw them outside the \foreach, and use the \foreach only for the axis ticks – CarLaTeX Oct 30 '18 at 7:16
  • @CarLaTeX Thank you. Please ignore my graph, and concentrate only in displaying the result of a style-defined number multiplication, to show 50 instead of 25*2, and so on. – Hany Oct 30 '18 at 7:18
6

You can do that with two simple \foreach:

\documentclass{beamer}
\beamertemplatenavigationsymbolsempty
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{frame}[t]
\frametitle{}
\begin{tikzpicture}[linecolor/.style={thick, blue}]
\draw [thick,-latex](0,0) -- (8,0);
\draw [thick,-latex](0,0) -- (0,6);
\foreach[evaluate=\myy as \myylab using int(\myy*2)] \myy in {1,2,...,5}{
\draw [linecolor] (0,\myy) +(180:.15cm) -- +(0:.15cm) node [xshift=-.68cm] {\myylab};}

\foreach[evaluate=\myx as \myxlab using int(\myx*25)] \myx in {1,2,...,7}{
\draw [linecolor] (\myx,0) +(90:1) -- +(-90:.25cm) node [yshift=-.35cm] {\myxlab};}
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

  • Thank you. But I want only to display the result of the multiplication of \inc, without affecting other components as the length of the lines. This is an MWE not the whole drawing. – Hany Oct 30 '18 at 6:38
  • @Hany Sorry, a typo, I'll correct immediately – CarLaTeX Oct 30 '18 at 6:46
  • @Hany See my renewed answer – CarLaTeX Oct 30 '18 at 6:48

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.