8

I have a simple figure with a code like this:

 \begin {figure}[htbp]
  \centering
  % 
  \begin{tikzpicture} [scale=1]
  \def \r {1/4} % y-scaling
  %
  % PARAMET.
  \def \a {1}   %x^6
  \def \b {-6}  %x^5
  \def \c {7}   %x^4
  \def \d {12}  %x^3
  \def \e {-17} %x^2
  \def \f {0}   %x^1
  \def \g {0}   % x^0
  %axis
  \draw[->,very thick](-4,0) -- (5,0) node [right] {x};
  \draw[->,very thick] (0,-3) -- (0,4) node [above] {y};
    %graph 
     \draw [variable=\t, color=blue, thick,domain= -1.5:3.2,samples=80] 
      plot (\t,    { \r*( \a*((\t)^6)+\b*((\t)^5)+\c*((\t)^4)+\d*((\t)^3)    +\e*((\t)^2)+\f*((\t))+\g) } );
\end{tikzpicture}
%
\caption 
{$y=x^6-6x^5+7x^4+12x^3-17x^2$}
\end{figure}

I want to change the parameters so that I can represents different polynomials. Is it possible to parametrize the caption in such a way that when I change the parameters and the caption change without having to rewrite it?

  • 2
    Put your \definitions outside the tikzpicture environment and inside the figure environment and use them in the argument of \caption. This is because the tikzpicture environment (in fact, every environment) is a group, so that \definitions inside such environment are local to that environment. In your code, \a, \b etc are undefined outside the tikzpicture environment. The suggested edit makes the whole figure as the scope of your macros and so the caption, too. – Pier Paolo Jul 25 '15 at 20:42
  • Thank you. It seems to work !. There is a way to avoid to have the terms $0 x^i$ if a parameter is null? – Emilio Novati Jul 25 '15 at 20:50

1 Answers1

8

You need to move your \defs outside the tikzpicture (otherwise, they don't survive the group created for the environment).

The caption is provided using

\caption{$y=\protect\polynomial{\g,\f,\e,\d,\c,\b,\a}$}

where \polynomial from the polynomial package gives the desired formatting for the polynomials. In fact, the code uses a redefinition of \polynomial suggested by egreg in his answer to How to delay expansion:

\usepackage{xparse}
\ExplSyntaxOn
\cs_set_eq:NN \gonzalo_poly:n \polynomial
\cs_generate_variant:Nn \gonzalo_poly:n { x }
\RenewDocumentCommand{\polynomial}{m}
 {
  \gonzalo_poly:x { #1 }
 }
\ExplSyntaxOff

The complete code:

\documentclass{article}
\usepackage{tikz}
\usepackage{polynomial}
\usepackage{xparse}

\ExplSyntaxOn
\cs_set_eq:NN \gonzalo_poly:n \polynomial
\cs_generate_variant:Nn \gonzalo_poly:n { x }
\RenewDocumentCommand{\polynomial}{m}
 {
  \gonzalo_poly:x { #1 }
 }
\ExplSyntaxOff

\begin{document}

\begin{figure}[!ht]
\def \r {1/4} % y-scaling
% PARAMET.
\def \a {1}   %x^6
\def \b {-6}  %x^5
\def \c {7}   %x^4
\def \d {12}  %x^3
\def \e {-17} %x^2
\def \f {0}   %x^1
\def \g {0}   %x^0
  \centering
  % 
  \begin{tikzpicture} [scale=1]
  %axis
  \draw[->,very thick](-4,0) -- (5,0) node [right] {x};
  \draw[->,very thick] (0,-3) -- (0,4) node [above] {y};
    %graph 
     \draw [variable=\t, color=blue, thick,domain= -1.5:3.2,samples=80] 
      plot (\t,    { \r*( \a*((\t)^6)+\b*((\t)^5)+\c*((\t)^4)+\d*((\t)^3)    +\e*((\t)^2)+\f*((\t))+\g) } );
\end{tikzpicture}
%
\caption{$y=\polynomial{\g,\f,\e,\d,\c,\b,\a}$}
\end{figure}

\begin {figure}[!hb]
\def \r {1} % y-scaling
% PARAMET.
\def \a {0}  %x^6
\def \b {0}  %x^5
\def \c {-2} %x^4
\def \d {0}  %x^3
\def \e {3}  %x^2
\def \f {0}  %x^1
\def \g {3}  %x^0
  \centering
  % 
  \begin{tikzpicture} [scale=1]
  %axis
  \draw[->,very thick](-4,0) -- (5,0) node [right] {x};
  \draw[->,very thick] (0,-1) -- (0,4) node [above] {y};
    %graph 
     \draw [variable=\t, color=blue, thick,domain= -1.5:1.5,samples=80] 
      plot (\t,    { \r*( \a*((\t)^6)+\b*((\t)^5)+\c*((\t)^4)+\d*((\t)^3)    +\e*((\t)^2)+\f*((\t))+\g) } );
\end{tikzpicture}
\caption{$y=\polynomial{\g,\f,\e,\d,\c,\b,\a}$}
\end{figure}

\end{document}

The result:

enter image description here

Gonzalo Medina
  • 505,128