3

I would like to make my Y axis non-linear. I have found an answer but the problem is that my code for charts is encapsulated in the \newcommand environment. When I use something like this:

\pgfplotsset{
        y coord trafo/.code={\pgfmathparse{ln(#1 + sqrt( #1^2 + 1))}\pgfmathresult},
        y coord inv trafo/.code={\pgfmathparse{#1}\pgfmathresult},
     }

The #1 argument is replaced by the command argument value. Is there a way to escape it or something to make this work?

MWE - taken and edited from the linked thread (works when the \newcommand is removed):

\newcommand{\makeChart}[1]{
\begin{filecontents*}{\jobname.dat}
    x, y
    0.000, -11.593
    1000.000, 394200.483
    0.029, -11.595
    500.015, 191387.137
    0.060, -11.598
    250.037, 89886.332
    0.092, -11.601
    125.065, 38883.652
    0.129, -11.604
    62.597, 12269.691
    0.188, -11.610
    31.393, 731.387
    0.676, -11.679
    16.034, -15.857
    23.713, -17.985
    27.553, -19.100
    29.473, 292.001
    27.671, -19.121
    28.572, 88.339 
    27.831, -19.150
    28.201, 5.087  
    28.124, -12.353
    28.178, -0.001
    28.179, 0.111
\end{filecontents*}
\begin{tikzpicture}
\pgfplotsset{
    y coord trafo/.code={\pgfmathparse{ifthenelse(#1<0,#1,ln((#1) + sqrt((#1)^2 + 1)))}},
 }
\begin{axis}[
    grid=major,
    xmin=0,
    xmax=1000,
    ytick={-20,-10,0,10,100},
    yticklabels={-20,-10,0,10,100},
    ]
    \addplot+[only marks] table[col sep=comma,x=x,y=y] {\jobname.dat};
\end{axis}
\end{tikzpicture}
}

\makeChart{something}
7
  • A MWE would be nice. Maybe you could save the content of #1 into a variable and use that instead?
    – Jan
    Jan 24 at 8:14
  • @Jan MWE is just whatever chart encapsulated in a \newcommand. Not sure about the variable. The #1 gets replaced in the whole command. I understand that in the trafo the #1 needs to be used as the argument of their own macro so there is a conflict. Like injecting our own code there right? Maybe I'm wrong though.
    – Hitokage
    Jan 24 at 8:46
  • @Jan Added the MWE after all....
    – Hitokage
    Jan 24 at 9:16
  • Can you have filecontents in the argument of a command? And what would be the reason for such complicated approach?
    – egreg
    Jan 24 at 9:22
  • @egreg Well I use the same chart type for many datasets and edit just some labels etc. so I have one command to create the chart that I use with different arguments. Not sure what you mean with the filecontents, the problem is that the #1 gets overwritten in the trafo code.
    – Hitokage
    Jan 24 at 9:32

2 Answers 2

4
  1. You cannot have filecontents* in the argument of another command; this would result in an empty file being written out.

  2. After you move filecontents* outside of the definition, simply change #1 into ##1 in the code for trafo, so this would refer to the value given to the key.

Example.

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

\begin{filecontents*}[overwrite]{\jobname.dat}
    x, y
    0.000, -11.593
    1000.000, 394200.483
    0.029, -11.595
    500.015, 191387.137
    0.060, -11.598
    250.037, 89886.332
    0.092, -11.601
    125.065, 38883.652
    0.129, -11.604
    62.597, 12269.691
    0.188, -11.610
    31.393, 731.387
    0.676, -11.679
    16.034, -15.857
    23.713, -17.985
    27.553, -19.100
    29.473, 292.001
    27.671, -19.121
    28.572, 88.339 
    27.831, -19.150
    28.201, 5.087  
    28.124, -12.353
    28.178, -0.001
    28.179, 0.111
\end{filecontents*}

\newcommand{\makeChart}[1]{%
  \begin{tikzpicture}
  \pgfplotsset{
    y coord trafo/.code={\pgfmathparse{ifthenelse(##1<0,##1,ln((##1) + sqrt((##1)^2 + 1)))}},
  }
  \begin{axis}[
    grid=major,
    xmin=0,
    xmax=1000,
    ytick={-20,-10,0,10,100},
    yticklabels={-20,-10,0,10,100},
    ]
    \addplot+[only marks] table[col sep=comma,x=x,y=y] {\jobname.dat};
  \end{axis}
  \end{tikzpicture}%
}

\begin{document}

\makeChart{something}

\end{document}

enter image description here

3

If you define a macro, the idea is (I think) to reuse that. So, you should never load packages etc. in a macro --- and most of the settings could be done outside. Moreover, your macro does not use the argument at all.

I interpreted your question like the following. I suppose you want to keep the y coord trafo inside the macro (although, unless it is different for each graph, is not needed at all) and I took the freedom to use the argument as the data file name. To have a literal #1 in the body of a macro, simply double the #: ##1.

You also ignored a couple of warnings that I take care of --- filecontents is in the kernel now, and compat= is needed for pgfplots.

\documentclass[border=2mm]{standalone}
\begin{filecontents*}[overwrite]{something.dat}
    x, y
    0.000, -11.593
    1000.000, 394200.483
    0.029, -11.595
    500.015, 191387.137
    0.060, -11.598
    250.037, 89886.332
    0.092, -11.601
    125.065, 38883.652
    0.129, -11.604
    62.597, 12269.691
    0.188, -11.610
    31.393, 731.387
    0.676, -11.679
    16.034, -15.857
    23.713, -17.985
    27.553, -19.100
    29.473, 292.001
    27.671, -19.121
    28.572, 88.339
    27.831, -19.150
    28.201, 5.087
    28.124, -12.353
    28.178, -0.001
    28.179, 0.111
\end{filecontents*}
\usepackage{pgfplots}\pgfplotsset{compat=1.18}
\usepackage{pgfplotstable}
\newcommand{\makeChart}[1]{
\begin{tikzpicture}
\pgfplotsset{
    y coord trafo/.code={\pgfmathparse{ifthenelse(##1<0,##1,ln((##1) + sqrt((##1)^2 + 1)))}\pgfmathresult},
    y coord inv trafo/.code={\pgfmathparse{##1}\pgfmathresult},
}
\begin{axis}[
    grid=major,
    xmin=0,
    xmax=1000,
    ytick={-20,-10,0,10,100},
    yticklabels={-20,-10,0,10,100},
    ]
    \addplot+[only marks] table[col sep=comma, x=x,y=y] {#1.dat};
\end{axis}
\end{tikzpicture}}

\begin{document}

\makeChart{something}

\end{document}

enter image description here

1
  • Thanks! I have marked the earlier answer as the accepted but this is basically the same!
    – Hitokage
    Jan 24 at 14: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.