-1

My question is more like ask for a common advice from more skilled people.

Let's say you write a manual covering some math or physics topics. You know TeX quite well, you can write complicated formulas, include images into you pdfs and aligning all of that as you want.

BUT

tex graphics, plotting etc - that's where I'm stucked. When you write the manual, you face the need to draw a graph of a random function for showing some properties. For example:

enter image description here

And you want to be able to generate such images quite often, for different tasks or function properties.

  1. Is there a way to create your own command to generate such axis (without ticks, with 0, OY on the left side, OX at the bottom, both of them started below 0)?

  2. What is the best way to draw a random function? To be able then add a point M on it and drop a perpendicular to Ox (without brute force of coordinates)

  3. Or maybe using tikzpictures is not the best way for writing the manual? Maybe drawing and stylizing in Inkscape or GeoGebra and then exporting to png is more convenient way

Thanks in advance

  • The same axis should be doable with a style, so you just copy the style name. You want a random function every time? So the plot is always different? Depending on the way you want it, might be hard to do. – Alenanno Oct 22 at 10:34
  • Does it matter if the plot changes almost every time you compile? – Alenanno Oct 22 at 10:46
  • No, it doesn't have to change every compiling. Just from plot to plot in pdf – Allok Oct 22 at 10:52
  • 1
    "arbitrary" would be a better word here than "random". Everything that you ask for is pretty straightforward in TikZ. I would encourage you to start working there. That being said, your question is really too broad. Once you have figured out some of this in TikZ, it'd be great to ask a question focusing on one aspect that's giving you trouble. – Teepeemm Oct 22 at 12:18
  • Teepeemm, yes, the question turned out too general. I thought, writing a manual is a common case, and maybe skilled writers could say "o, no, programming tikzpicture for that is too complicated. I prefer Geogebra" or otherwise "I use some custom tikz commands for that and this" – Allok Oct 22 at 12:27
3

Here is a starting point for you to work on.

The current version gives you a command \randplot that you can slap on the document, it's already embedded in a figure but you can change that if you prefer.

The \pgfmathsetseed{1337} line is the seed for the random numbers, but since we wrote it explicitly, plots will never change regardless of how many times you compile, unless you change this number or remove the line.

Addition: I have added the dashed lines and the intersections, plus the secant line.

Detail of the first graph

enter image description here

Output (part view of A4 page)

enter image description here

Code

\documentclass[10pt]{article}
\usepackage[a4paper, margin=2cm]{geometry} 
\usepackage{tikz,pgfplots}

\pgfplotsset{compat=1.17} 
\pgfmathsetseed{1337}

\usetikzlibrary{intersections}

\newcommand\randplot{%
    \begin{figure}[!h]
        \centering
    \begin{tikzpicture}
        \pgfmathsetmacro\firstX{random(1.2,3.3)}
        \pgfmathsetmacro\secondX{random(3.5,5.3)}
        \begin{axis}[%
                scale only axis,
                axis lines=middle,
                xlabel={\large $x$},
                ylabel={\large $y$},
                ticks=none,
                ymin=-1,
                ymax=8,
                xmin=-2,
                xmax=6,
                tick style={draw=none},
            ]
            \coordinate (O) at (axis cs:0,0);
            \coordinate (X) at (axis cs:6,0);
            \addplot[orange,thick,smooth, name path=plot] {x*x^rand};
            %
            \draw[dashed, name path=first] (axis cs:\firstX,0) -- (axis cs:\firstX,5)
                node[pos=0, below, anchor=north] {$X_0$}
            ;
            \draw[dashed, name path=second] (axis cs:\secondX,0) -- (axis cs:\secondX,5)
                node[pos=0, below, anchor=north] {$X_0+\Delta X$}
            ;

            \fill[orange!80, draw=black]
                [name intersections={of=plot and first,by={M}}]
                [name intersections={of=plot and second,by={M1}}]
                (M) circle (2pt)
                (M1) circle (2pt)
                ;

            \draw[dashed] (O|-M) -- (M-|X)
                node[pos=0, left, anchor=east] {$Y_0$}
            ;
            \draw[dashed] (O|-M1) -- (M1-|X)
                node[pos=0, left, xshift=-5mm, anchor=east] {$Y_0+\Delta Y$}
            ;

            \draw[densely dashed, orange, shorten <=-1cm, shorten >=-1cm] (M) -- (M1);
        \end{axis}
    \end{tikzpicture}
    \end{figure}
}
\begin{document}\noindent
\randplot

A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text A lot of text.\par

\randplot
\end{document}
| improve this answer | |
  • cool! how can I work with the specific point on such plot (inside {tikzpicture} environment}? for example, I want to mark a thick point M on the graph and drop a perpendicular to Ox, how can I do that? – Allok Oct 22 at 12:20
  • @Allok I will take a look at that later. :) – Alenanno Oct 22 at 12:54
  • @Allok I have added more details. – Alenanno Oct 22 at 13:51
  • Thanks, I've got some come tips to study – Allok Oct 22 at 15:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.