I am using a tikzpicture
environment to draw a Temperature v composition plot. I do not have exact data so am only mimicking it.
I am aware that I could/should use pgfplots
but the issue could be instructive…
I draw two axes of length 10 units each (from 0-10) and I want to synthesize axes ticks at certain points on these axes.
I want to automatically calculate a numerical value and put it in the node
text.
The actual calculation for the temperature is T=272.5+2.5*\y
For the composition, it is \upchi=0.1*\x
I understand how to do a node
position
calculation but not how to calculate a numerical value to put into the node
text
.
I can do it manually as in the code following:
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{upgreek}
\usepackage{siunitx}
\begin{document}
\begin{tikzpicture}
\draw[step=0.1cm,black!10,very thin] (-0.1,-0.1) grid (10.2,10.2);%setting up grid and axes
\draw[step=1cm,black!50,help lines] (-0.1,-0.1) grid (10.2,10.2);
\draw[thick,->] (0,0) -- (10.2,0);
\draw[thick, ->] (0,0) -- (0,10.2);
\foreach \x in {0,1,...,10}
\draw[thick] (\x,-2pt) -- (\x,2pt);%drawing tick marks
\foreach \x in {0,2,...,8}
\draw node at (\x,-10pt) {$0.\x$};% attempt to write node of 0.2, 0.4 etc
%\foreach \x in {0,2,...,10}
%\draw node at (\x, -10pt) {$(0.1*\x)$}; % what I thought I could do...
%\foreach \y in {1,3,...,9}
%\draw node at (-15pt,\y) {$(2.5*\y+272.5)$}; % what I thought I could do...
\draw node at (10,-10pt){$1.0$};
\draw node at (9,-25pt) {Mole Fraction $\upchi_{NB}$};
\draw node at (-15pt, 1) {275};
\draw node at (-15pt, 3) {280};
\draw node at (-15pt, 5) {285};
\draw node at (-15pt, 7) {290};
\draw node at (-15pt, 9) {295};
\draw node at (-40pt, 5)[rotate=90]{Temperature[$\si{\celsius}$]};
\draw node at (5,3.2){2 Phases};
\draw node at (1,6.2)[rotate=0]{1 Phase};
\draw node at (9,7.2)[]{1 Phase};
\foreach \y in {0,1,...,10}
\draw[thick](-2pt,\y) -- (2pt,\y);
\draw[very thick] plot[smooth] coordinates{(0.5,0) (2,5)(4,8) (6,9)(7,8.5)(8.5,5)(9,0) };
\end{tikzpicture}
\end{document}
Best Answer
You need to pass the expression through a math parser first in order to evaluate it. One way of doing this is to use
\pgfmathparse{<expression>}
, and then printing the result using\pgfmathprintnumber[<optional number formatting keys>]{\pgfmathresult}
: