4

I would like to have the data series for a sparkline defined in terms of a macro -- or some alternative -- as opposed to have it set explicitly in the sparkline environment itself. My attempts so far have failed. As an MWE, the following would do:

\documentclass[12pt]{memoir}

\usepackage{xcolor,tikz,sparklines}

\begin{document}
\xdef\SparkMacro{0 1 0.5 1.5 1 0.25} 
\renewcommand{\sparklineheight}{1.75}
\begin{sparkline}{3}
\sparkrectangle 0 1.75
\spark \SparkMacro\ / % doesn't work
% \spark 0 1 0.5 1.5 1 0.25 /% does work
\end{sparkline}
\end{document}

How could I have those data passed to the environment sparklinewithout having to type (or copy and paste) them explicitly?

diabonas
  • 25,784
Marcos
  • 5,422

2 Answers2

6

You need \expandafter to expand the data before \spark sees it. Also you need a real space not \ at the end. The easiest is to make sure your macro ends with space.

\documentclass[12pt]{memoir}

\usepackage{xcolor,tikz,sparklines}

\begin{document}
\xdef\SparkMacro{0 1 0.5 1.5 1 0.25 } 
\renewcommand{\sparklineheight}{1.75}
\begin{sparkline}{3}
\sparkrectangle 0 1.75
\expandafter\spark \SparkMacro / % doesn't work
%\spark 0 1 0.5 1.5 1 0.25 /% does work
\end{sparkline}
\end{document}
Aditya
  • 62,301
David Carlisle
  • 757,742
  • Right. I had it in front of me, but my mind was not clear about the spaces and got into a mess with several \expandafter commands. Thank you so much. – Marcos Sep 02 '12 at 17:04
  • Another alternative is to not include the space in SparkMacro but use \SparkMacro{} / – Aditya Sep 02 '12 at 17:27
6

In Is there a package that provides graphing in the style of Ed Tufte? I created a solution for sparklines which is not based on the sparkline package, but on pgfplots.

The advantage is: you can provide input by means of numerical tables, provide a numerical table and plot data based on mathematical expressions on those tables, or provide a mathematical expression right-away.

The mentioned solution features bar plots, line plots, and scatter plots, and it can also determine the minimum/maximum coordinate automatically.

See Is there a package that provides graphing in the style of Ed Tufte? for details

  • 1
    Thanks so much for pointing this out. I already knew of the package and I think it is a great contribution to graphics integration in TeX-based documents, even though I am more familiar with PSTricks-based graphics generation -- just from my regular use of them. I'm sure that I'll go to Tikz-based methods for some tasks, though. – Marcos Sep 12 '12 at 19:11