6

How can I tell tikz to make a calculation within a matrix. The same syntax outside does not work within as is shown in this MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.8, declare function={f(\x)=1/ln(2)*ln(\x);}]
\draw[step=1.0,gray,thin,dotted] (-.5,-3) grid (8.5,4);
\draw [-latex] (-.5,0) -- (8.5,0) node (xaxis) [below] {$x$};
\draw [-latex] (0,-3.5) -- (0,4) node [left] {$y$};
   \foreach \x/\xtext in {1/1,2/2,3/3,4/4,5/5,6/6}
   \draw[xshift=\x cm] (0pt,3pt) -- (0pt,0pt) 
     node[below=2pt,fill=white,font=\normalsize]
  {$\xtext$};    
   \foreach \y/\ytext in {-2/-2,-1/-1, 1/1,2/2,3/3}
     \draw[yshift=\y cm] (2pt,0pt) -- (-2pt,0pt) 
    node[left,fill=white,font=\normalsize]
    {$\ytext$};
\draw[name path=curve,domain=.17:8.5,samples=200,variable=\x,red,<->,thick] 
   plot ({\x},{f(\x)});
 \draw[fill=red,red] (1,{f(1)}) circle (3pt) node[] {};
 \draw[fill=red,red] (2,{f(2)}) circle (3pt) node[] {};
 \draw[fill=red,red] (4,{f(4)}) circle (3pt) node[] {};
 \draw[fill=red,red] (8,{f(8)}) circle (3pt) node[] {};
 \draw[fill=red,red] (.5,{f(.5)}) circle (3pt) ;
 \draw[fill=red,red] (.25,{f(.25)}) circle (3pt) node[] {$$};
 \node at (3.5,3.5) [text=red,fill=white] {$f(x)=\log_{2} x,\ \text{or}\ 2^{y}=x$};
 \matrix[matrix of math nodes,nodes={align=center,inner sep=3pt,
  text height=1.5ex,text depth=.25ex,draw=gray!40,ultra thin},draw,inner 
   sep=0pt,ampersand replacement=\&] (mat1)
    at (-5,0){
   |[fill=green!40!gray,text width=15mm]| x,\ \text{or}\ 2^{y}  \& | 
  [fill=green!40!gray,text width=11mm]| f(x) \\ 
                 |[text width=15mm]|   1                      \& |[text  
     width=11mm]|  0\\
                                     |[text width=15mm]|   2                      
  \& |[text width=11mm]|  1\\
                                     |[text width=15mm]|   4                      
  \& |[text width=11mm]|  2\\
                                     |[text width=15mm]|   8                      
    \& |[text width=11mm]|  {f(3)}3\\
                                     |[text width=15mm]|   \frac{1}{2}                      
  \& |[text width=11mm]|  -1\\
                                     |[text width=15mm]|   \frac{1}{4}                      
  \& |[text width=11mm]|  -2\\
                                    };
\end{tikzpicture}
\end{center}
\end{document}

This outputs

enter image description here

2
  • 2
    Replace {f(3)} in the matrix by {\pgfmathparse{f(3)}\pgfmathprintnumber{\pgfmathresult}} or, which seems more appropriate, {\pgfmathparse{f(8)}\pgfmathprintnumber{\pgfmathresult}}?
    – user121799
    Feb 5, 2019 at 17:25
  • @Marmot. I was just going to try that! Thank Marmot! Yes it works. Feb 5, 2019 at 17:26

1 Answer 1

7

I'll be happy to remove this if you want to remove your question, or feel it is not appropriate.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}
\usetikzlibrary{intersections}

\begin{document}
\begin{center}
\begin{tikzpicture}[scale=.8, declare function={f(\x)=1/ln(2)*ln(\x);}]
\draw[step=1.0,gray,thin,dotted] (-.5,-3) grid (8.5,4);
\draw [-latex] (-.5,0) -- (8.5,0) node (xaxis) [below] {$x$};
\draw [-latex] (0,-3.5) -- (0,4) node [left] {$y$};
   \foreach \x/\xtext in {1/1,2/2,3/3,4/4,5/5,6/6}
   \draw[xshift=\x cm] (0pt,3pt) -- (0pt,0pt) 
     node[below=2pt,fill=white,font=\normalsize]
  {$\xtext$};    
   \foreach \y/\ytext in {-2/-2,-1/-1, 1/1,2/2,3/3}
     \draw[yshift=\y cm] (2pt,0pt) -- (-2pt,0pt) 
    node[left,fill=white,font=\normalsize]
    {$\ytext$};
\draw[name path=curve,domain=.17:8.5,samples=200,variable=\x,red,<->,thick] 
   plot ({\x},{f(\x)});
 \draw[fill=red,red] (1,{f(1)}) circle (3pt) node[] {};
 \draw[fill=red,red] (2,{f(2)}) circle (3pt) node[] {};
 \draw[fill=red,red] (4,{f(4)}) circle (3pt) node[] {};
 \draw[fill=red,red] (8,{f(8)}) circle (3pt) node[] {};
 \draw[fill=red,red] (.5,{f(.5)}) circle (3pt) ;
 \draw[fill=red,red] (.25,{f(.25)}) circle (3pt) node[] {$$};
 \node at (3.5,3.5) [text=red,fill=white] {$f(x)=\log_{2} x,\ \text{or}\ 2^{y}=x$};
 \matrix[matrix of math nodes,nodes={align=center,inner sep=3pt,
  text height=1.5ex,text depth=.25ex,draw=gray!40,ultra thin},draw,inner 
   sep=0pt,ampersand replacement=\&] (mat1)
    at (-5,0){
   |[fill=green!40!gray,text width=15mm]| x,\ \text{or}\ 2^{y}  \& | 
  [fill=green!40!gray,text width=11mm]| f(x) \\ 
                 |[text width=15mm]|   1                      \& |[text  
     width=11mm]|  0\\
                                     |[text width=15mm]|   2                      
  \& |[text width=11mm]|  1\\
                                     |[text width=15mm]|   4                      
  \& |[text width=11mm]|  2\\
                                     |[text width=15mm]|   8                      
    \& |[text width=11mm]| 
    {\pgfmathparse{f(8)}\pgfmathprintnumber{\pgfmathresult}}\\
                                     |[text width=15mm]|   \frac{1}{2}                      
  \& |[text width=11mm]|  -1\\
                                     |[text width=15mm]|   \frac{1}{4}                      
  \& |[text width=11mm]|  -2\\
                                    };
\end{tikzpicture}
\end{center}
\end{document}

enter image description here

1
  • 2
    no it is okay to leave. It may be helpful to someone else! Feb 5, 2019 at 17:34

You must log in to answer this question.

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