1

I would like to create a specific macro in order to make graph display easier using Tikz.

For this purpose, I am trying to make an environment which uses many parameters. For instance, I would like to have xMin, xMax, yMin and yMax to define from where to where to display my graph, a parameter to describe where to put the legend (below or above the axes) and so on.

Therefore, I would like to make a call like this

\begin{graphe}[xMin=-5,xMax=5,yMin=5,
yMax=5,xAxisRight=below,xAxisLeft=below,yAxisTop=right,
yAxisBottom=right,xStep=1,yStep=1,gridStep=5,xScale=1,yScale=1]
\end{graphe}

I am using xString to get the values of my different parameters. My problem is the following : when I do

\StrBetween{#1}{xMin=}{,}

for instance, I do get the "-5" as expected but this is a string chain. Therefore, using this in any Tikz call will cause an error.

Although I made quite some research on the internet, I couldn't find any way to parse the string "-5" into the number -5.

I am sure I missed something and that there must be a very easy solution which I failed to find.

Thanks in advance for the help !

Edit :

\newenvironment{graphe}[1][]
{
\StrBetween{#1}{xMin=}{,}
\newcommand{\xMin}{-5}
\newcommand{\yMin}{-5}
\newcommand{\xMax}{5}
\newcommand{\yMax}{5}
\newcommand{\xAxisRight}{above}
\newcommand{\xAxisLeft}{above}
\newcommand{\yAxisTop}{right}
\newcommand{\yAxisBottom}{right}
\newcommand{\xStep}{1}
\newcommand{\yStep}{1}
\newcommand{\quadrillageStep}{5}
\newcommand{\xScale}{1}
\newcommand{\yScale}{1}
\renewcommand{\xMin}{\StrBetween{#1}{xMin=}{,}}
\begin{center}
\begin{tikzpicture}[xscale=1]
\draw[->,>=latex,very thick] (0,0) -- (\xMax,0);
}
{
\end{tikzpicture}
\end{center}
}

The last call

\draw[->,>=latex,very thick] (\xMin,0) -- (\xMax,0);

does generate an error but If I do the call with -5 instead of \xMin it works. Therefore I thought that this was a conversion problem.

  • 3
    Why don't you use tikzset (because you tagged tikz) or better pgfkeys ? This is the exact reason why they are invented. – percusse Nov 7 '17 at 20:59
  • 2
    tex has neither strings nor numbers in either case you just get the pair of tokens - and 5 but \StrBetween{#1}{xMin=}{,} is not a list of tokens it expands to a sequence of definitions that would eventually typeset the result so it fails just as using zz=\def\foo{1}\foo will fail and not work zz=1 – David Carlisle Nov 7 '17 at 21:00
  • 2
    Perhaps it would be better to show your use-case. That way one can better assist you in providing solutions to the actual problem. – Werner Nov 7 '17 at 21:04
  • I did add the environnment description as it is now. Sorry, but I do not understand the first 2 answers, I am really not an expert in LaTex, I am trying my best but many of the subtleties remain totally misterious to me sadly. – HerculePoivrot Nov 7 '17 at 21:12
  • 1
    @HerculePoivrot: So you don't put anything between \begin{graphe}...\end{graphe}. Only some stuff in the optional argument? – Werner Nov 7 '17 at 21:17
1

You can use \pgfkeys (or \tikzset if you prefer, although it is almost equivalent), to set defaults and process your settings. The following code fixes and slightly extends your MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}

% Using pgfkeys makes it easier to use key-value settings for the graph
\pgfkeys{/graphe/.is family, /graphe,
  xmin/.initial        = -3,    % defaults for xmin, xmax, ymin,ymax
  xmax/.initial        =  3,
  ymin/.initial        = -3,
  ymax/.initial        =  3,
  ylabel/.initial      = f(x),% default function name
  scale/.initial       = 0.9,  % tikzpicture scale
  xtics/.initial       = {1},  % list of marked coordinates on x-axis
  ytics/.initial       = {1},   % list of marked coordinates on y-axis
  xAxisRight/.initial  = below,
  xAxisLeft/.initial   = below,
  yAxisTop/.initial    = right,
  yAxisBottom/.initial = right,
  xStep/.initial       = 1,
  yStep/.initial       = 1,
  gridStep/.initial    = 5,
  xscale/.initial      = 1,
  yscale/.initial      = 1
}

% shortcut to access values of /graphe
\newcommand\Gval[1]{\pgfkeysvalueof{/graphe/#1}}

% graph environment with optional argument for changing defaults
\newenvironment{graphe}[1][]%
   {\begin{center}
    \pgfkeys{/graphe, #1}% process settings
    \begin{tikzpicture}[xscale=\Gval{scale}, % set tikzpicture options
                        yscale=\Gval{yscale}]
    }
    {\end{tikzpicture}
     \end{center}}

\begin{document}

  \begin{graphe}
    \draw[->,>=latex,very thick] (\Gval{xmin},0) -- (\Gval{xmax},0);
  \end{graphe}

  \begin{graphe}[xmax=6]
    \typeout{xmin = \Gval{xmin}, xmax=\Gval{xmax}.}
    \draw[->,>=latex,very thick] (\Gval{xmin},0) -- (\Gval{xmax},0);
  \end{graphe}

\end{document}

As shown, to access any of the key-values inside the graphe environment you use the "helper function" \Gval{...}. The way that it is set up you can set default values for each of your keys (which is called the initial value by \pgfkeys). You only need to specify key values when they differ from the default values. The settings given to the graphe environment will only affect that instance of graphe.

For completeness, here is the expected but not very exciting output:

enter image description here

Note that, as in the original post, the tikzpicture environments are centered.

  • Thanks very much for taking the time to answer this question. Your solution works great. I have just one last question because I like to learn from my mistakes : why does \draw[->,>=latex,very thick] (\StrBetween{xMin=-5}{xMin=}{,},0) -- (5,0); generate an error when \StrBetween{xMin=-5}{xMin=}{,} produces -5 as a result. I do not understand that. – HerculePoivrot Nov 8 '17 at 6:36
  • @HerculePoivrot The official way to thank is to update and award green ticks:) I've never played with the xstring package so I don't really know but I would guess that it is an expansion issue. I haven't tested this, but probably \xdef\Draw{\noexpand\draw[->,>=latex,very thick] (\StrBetween{xMin=-5}{xMin=}{,},0) -- (5,0);}\Draw will work as it forces \StrBetween to expand before it is seen by the tikz \draw command. – Andrew Nov 8 '17 at 10:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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