2

The following is a MWE of a plot reading the xticklabels from a file. It works, but the comma's inside the string that is fetched from the file, don't split the string into multiple parts. How do we remove the group around the string that is fetched?

\documentclass{article}
\usepackage{pgfplots, datatool, filecontents}
\begin{filecontents}{myfile.tex}
xticklabels:-5,-4,-3,-2,-1,0,1,2,3,4,5
\end{filecontents}
\DTLsetseparator{:}
\begin{document}
\DTLloaddb[noheader, keys={key,value}]{mydata}{mydata.extra}
\begin{tikzpicture}
    \begin{axis}[
        xtick = {-5,-4,-3,-2,-1,0,1,2,3,4,5},
        xticklabels = \DTLfetch{mydata}{key}{xticklabels}{value},
    ]
    \addplot {x^2};
    \end{axis}
  \end{tikzpicture}
\end{document}

The top image is what we get, and the bottom image is what I expect.

enter image description here

1

pgfplots, with its companion package pgfplotstable, already has the ability to read data from file. (And I guess/think datatool has some compatibility issue with pgfplots.)

In the following example,

  • Data is written in mydata.tex by row, and separated by comma.
  • Data is stored in command \mydata.
    • Since pgfplots reads data by column, \pgfplotstabletranspose is used to transpose the data matrix.
    • By default, \pgfplotstabletranspose creates new column names. Options colnames from={xticklabels}, input colnames to={xticklabels} make it use xticklabels as column name.
  • Option xticklabels from table={\mydata}{xticklabels} uses data from table \mydata, column xticklabels as xticklabels list.

Related questions:

\begin{filecontents}[force, noheader]{mydata.tex}
xticklabels,-5,-4,-3,-2,-1,0,1,2,3,4,5
\end{filecontents}

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\pgfplotstableset{col sep=comma}

\begin{document}
\begin{tikzpicture}
  % transpose data table
  \pgfplotstabletranspose[
    colnames from={xticklabels}, 
    input colnames to={xticklabels}
  ]{\mydata}{mydata.tex}
  
  \begin{axis}[
      xtick = {-5,-4,-3,-2,-1,0,1,2,3,4,5},
      xticklabels from table={\mydata}{xticklabels},
  ]
    \addplot {x^2};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

As a response to this comment

In case one wants to have syntax xticklabels=<macro storing a list>, see the following patch

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
\makeatletter
% redefine pgfplots key "xticklabels", expand its value once before parsing
\pgfkeys{
    /pgfplots/xticklabels/.code={%
        \expandafter\pgfplotslistnew\expandafter\pgfplots@xticklabels\expandafter{#1}%
        \let\pgfplots@xticklabel=\pgfplots@user@ticklabel@list@x
    }
}
\makeatother

\newcommand\xticklabels{-5,-4,-3,-2,-1,0,1,2,3,4,5}
\begin{tikzpicture}
  \begin{axis}[
      xtick = {-5,-4,-3,-2,-1,0,1,2,3,4,5},
      xticklabels=\xticklabels,
  ]
    \addplot {x^2};
  \end{axis}
\end{tikzpicture}
\end{document}
6
  • Thank you for this alternative solution using pgfplotstable. I was aware of this package but actually interested in a solution without using a different package. Appearantly even xticklabels = \xticklabels with \newcommand\xticklabels{-5,-4,-3,-2,-1,0,1,2,3,4,5} does not work. Why not? – Carucel Aug 4 '20 at 11:25
  • @Carucel Because key xticklabels is defined such that its value is not expanded before being parsed as a list. – muzimuzhi Z Aug 4 '20 at 12:09
  • @Carucel To support syntax xticklabels=<macro storing a list>, I've added a new example to my answer. – muzimuzhi Z Aug 4 '20 at 12:17
  • Thank you! Is there a way to expand all? \newcommand\xticklabelsB{-5,-4,-3,-2,-1,0,1,2,3,4,5}\newcommand\xticklabels{\xticklabelsB} does not work using your solution. – Carucel Aug 4 '20 at 17:33
  • 1
    @Carucel It's feasible, for example use \expandafter\pgfplotslistnew\expandafter\pgfplots@xticklabels\expandafter{\expanded{#1}}. Be care because this requires the ticklabel list being fully expandable. Alternatively, you can expand \xticklabelsB when defining \xticklabels, \expandafter\newcommand\expandafter\xticklabels\expandafter{\xticklabelsB}. – muzimuzhi Z Aug 4 '20 at 19:04

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.