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


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


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 :

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

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 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. 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

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:


% 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

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


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

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


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. 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.
    – user30471
    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.