1

I would like to plot some physichs diagram with the plot command in LaTeX. However i ran into multiple issues during my journey, and im currently stuck. Maybe i am pushing the limits on tikz's computational skills too hard, but you never know... Im using this method over pgfplot's axis and \addplot method, because this is more costumizable in my opinion. I'd apprichiate some help. Thx <3 (I included my code, and the desired result, plotted with Python. hope it helps.)

\documentclass[12pt, a4paper]{article}
\usepackage{amsmath, tikz, pgfplots, xfrac, pst-calculate}
\usepackage[top=1in, bottom=1in, left=1.25in, right=1.25in]{geometry}
\pagestyle{empty}
\pgfplotsset{compat=1.18}
\usetikzlibrary {arrows.meta}
\tikzset{every picture/.style={line width=0.25mm}}

\def\b{2.9}

%domain x1, x2, func
\newcommand{\myplot}[3]{
    \fill [pink, domain=#1:#2, variable=\x]
    (#1, 0)
    -- plot ({\x},{#3})
    -- (#2, 0)
    -- cycle;
    \draw [domain=#1:#2, variable=\x] plot ({\x},{#3});}


\begin{document}
\centering
\begin{tikzpicture}    
    \node at (0,1) {\pscalculate{(11948079.123643*((\b/2)*0.01)**2 - 2512.08363574594)/1000}};
    \node at (0,0) {\pscalculate{(11948079.123643*((\b/2-\b/6)*0.01)**2 - 2512.08363574594)/1000}};

\begin{scope}[xshift=6.5cm]
    \begin{scope}[rotate=90]%this is the problematic part:
        \myplot{\b/2}{\b/2-\b/6}{(11948.079123643*(\x*0.01)**2 - 2.51208363574594)}
    \end{scope}

    %axis
    \draw[-{Stealth}] (-1.5,0)--(1.5,0) node[right] {$\sigma_{krit}^{(1)}$[MPa]};
    \draw[-{Stealth}] (0,-2)--(0,2.5) node[left] {$z$};

    %ticks
    \foreach \x in {-1,0.5}
        \draw[xshift={\x cm}, line width=0.1mm] (0,-0.05)--(0,0.05) node[below=0.05] {$\pscalculate{\x*10}$};

    \foreach \x in {-0.5,1}
        \draw[xshift={\x cm}, line width=0.1mm] (0,-0.05)--(0,0.05) node[above=0.05] {$\pscalculate{\x*10}$};
\end{scope}

\end{tikzpicture}
\end{document}

enter image description here

Update: The following code worked for me:

\myplot{\b/2}{\b/2-\b/6}{(1.1948079123643*(\x)^2 - 2.51208363574594)}

enter image description here

It seems that the problem with my previous approach was the "**" instead of the "^" symbol. Having said that, if you compile the following code, you will get a weird function, which makes a staircase-like graph. Maybe it is so small, itt reaches the end of tikz's calculating capabilities.

\myplot{\b/2}{\b/2-\b/6}{(11948.079123643*(\x*0.01)^2 - 2.51208363574594)}

enter image description here Also, i would like to point out, that I did not rebind the \b macro, but only because im a lazy person, and you shouldn't do such things. Thanks for helping me <3

4
  • Welcome. // Looks like you mix pgfplots and tikz in a way it‘s not intended. E.g. with pgfplots you mainly use \addplot of various kinds, e.g. to display scientific data. I don’t see any \addplot in your code. With Tikz your colored area is easy to draw, while you also have to draw axis, ticks and labels, and grid. Not sure what the horizontal lines shall mean: do you want a pattern? Not sure, what your drawing is intended to tell. Not sure, what your code produces.
    – MS-SPO
    Commented Apr 3 at 2:49
  • 1
    Off-topic, probably: \def\b{2.9} is a Really, Really Bad Idea. What if you need the original definition?
    – cfr
    Commented Apr 3 at 4:27
  • 1
    You have double *s in multiple places. That may be valid for pst, but it isn't valid for pgf. The one you've asked pgf to calculate causes the compilation failure.
    – cfr
    Commented Apr 3 at 4:47
  • 1
    Honestly, this isn't an amazing question, but it is not at all bad and certainly doesn't deserve a downvote. It provides code, it reports the error and it makes an attempt to explain what is wanted. OK, the attempt isn't altogether successful but what do you people expect? This is the first post of an OP who only joined today. I hope whoever voted this down receives a decidedly chilly reception next time s/t/he/y dares venture into new territory!
    – cfr
    Commented Apr 3 at 5:29

1 Answer 1

0

I don't know what your code is trying to do. The result doesn't seem to correlate with the image you show. However, it is possible to diagnose and correct the cause of your immediate problem i.e. the error.

** is acceptable in expl3 syntax, but it is not acceptable to pgf. When you're using \pst{} to output a result inside the first two nodes, there's no problem, but when you come to draw the plot, you're asking pgf to calculate the function and the use of ** causes the error.

The solution is simply to replace ** with ^, which is supported by both expl3 and pgf. Then your code compiles.

You should also not say \def\b which overwrites the standard LaTeX macro \b. If you must use \def, try to use it inside a tikzpicture where the potential harm is limited in scope. Or use \newcommand\name{} before \def\name{} so you at least get an error if \name is already used. In general, single-letter macros should be avoided outside restricted self-limiting contexts.

You aren't using pgfplots, so remove it unless you need it for something else.

Here is 'working' code in the limited sense that it (1) compiles without error and (2) doesn't --- to the best of my knowledge --- overwrite any existing LaTeX or TeX macros.

\documentclass[12pt, tikz]{standalone}
\usepackage{amsmath, pst-calculate}
\usetikzlibrary {arrows.meta}
\tikzset{every picture/.style={line width=0.25mm}}
\newcommand\bnew{}% check the name isn't used
\def\bnew{2.9}% if we didn't get an error, we know this is safe
% \newcommand\bnew{2.9}% two-for-one special

%domain x1, x2, func
\newcommand{\myplot}[3]{%
    \fill [pink, domain=#1:#2, variable=\x]
    (#1, 0)
    -- plot ({\x},{#3})
    -- (#2, 0)
    -- cycle;
    \draw [domain=#1:#2, variable=\x] plot ({\x},{#3});}

\begin{document}
\centering
\begin{tikzpicture}    
  \node at (0,1) {\pscalculate{(11948079.123643*((\bnew/2)*0.01)^2 - 2512.08363574594)/1000}};% keep syntax consistent
  \node at (0,0) {\pscalculate{(11948079.123643*((\bnew/2-\bnew/6)*0.01)^2 - 2512.08363574594)/1000}};
  
  \begin{scope}[xshift=6.5cm]
    \begin{scope}[rotate=90]% replace unrecognised syntax
      \myplot{\bnew/2}{\bnew/2-\bnew/6}{(11948.079123643*(\x*0.01)^2 - 2.51208363574594)}
    \end{scope}
    
    %axis
    \draw[-{Stealth}] (-1.5,0)--(1.5,0) node[right] {$\sigma_{krit}^{(1)}$[MPa]};
    \draw[-{Stealth}] (0,-2)--(0,2.5) node[left] {$z$};
    
    %ticks
    \foreach \x in {-1,0.5}
    \draw[xshift={\x cm}, line width=0.1mm] (0,-0.05)--(0,0.05) node[below=0.05] {$\pscalculate{\x*10}$};
    
    \foreach \x in {-0.5,1}
    \draw[xshift={\x cm}, line width=0.1mm] (0,-0.05)--(0,0.05) node[above=0.05] {$\pscalculate{\x*10}$};
  \end{scope}
\end{tikzpicture}
\end{document}

Fortunately, I can't post images right now due to an Okular bug which makes them all suck. However, in this case, believe me, you aren't missing anything as the result looks nothing like the target (not even as an overly optimistic, aspiration).

4
  • 1
    I will probably delete this later, so don't waste your time voting for it (or against it).
    – cfr
    Commented Apr 3 at 5:19
  • 1
    Thank you, this solved my problem. Commented Apr 3 at 15:18
  • OK, I'm not allowed to delete it. But it's a mess anyhow.
    – cfr
    Commented Apr 3 at 16:04
  • @DömeKálmánSarus Oh, good. I thought it probably wouldn't but might help start somebody else answering. (My output looked nothing like your image, so I figured it wasn't a proper answer.) And ignore the downvote.
    – cfr
    Commented Apr 3 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .