3

Straight forward question:

\newcommand{\optaxis}[1][]{
   \ifemptyarg{#1}
    {\begin{axis} [xtick={0},   xmin=-1,  xmax =1,  xticklabels={#2},   box plot width=5mm]}
    {\begin{axis} [xtick={0,1}, xmin=-1,  xmax =2,  xticklabels={{#1},{#2}},    box plot width=5mm]}%

}

How can I get the part here to work correctly - {{#1},{#2}} - I am getting a variety of errors which I cannot seem to resolve. If I change that to {test,test2} it works and compiles fine.

The specific error message is:

You meant to type ## instead of #, right?


edit:

I am trying to use from here:

\makeatletter


\def\ifemptyarg#1{%
  \if\relax\detokenize{#1}\relax % H. Oberdiek
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}

\makeatother

\newcommand{\optaxis}[1][]{
   \ifemptyarg{#2}
    {\begin{axis} [xtick={0},   xmin=-1,  xmax =1,  xticklabels={#1},   box plot width=5mm]}
    {\begin{axis} [xtick={0,1}, xmin=-1,  xmax =2,  xticklabels={{#1},{#2}},    box plot width=5mm]}
}

I want to be able to call this like:

\optaxis{test1}
\optaxis{test1, test2}

and have each called respectively.

4
  • 2
    I answered the specific error but if you made your example into a complete (small) document that showed what you were trying to do we could suggest how to change the code. Commented Mar 29, 2013 at 22:25
  • @DavidCarlisle I've updated the question with more.
    – enderland
    Commented Mar 29, 2013 at 22:29
  • There is only one parameter in \optaxis{test1, test2} which is test1, test2. If you used \optaxis{test1}{test2} then you would have two parameters. Commented Mar 29, 2013 at 22:30
  • See my answer, you just want one parameter which can have a comma, you have defined it as optional so my examples use [] if you do not define it as optional then the argument would be surrounded by {} Commented Mar 29, 2013 at 22:33

1 Answer 1

8

You have defined a command with one argument

\newcommand{\optaxis}[1][]{

so you can't use #2

I suspect you just want

\newcommand{\optaxis}[1][]{%%%
 \begin{axis} [xtick={0},   xmin=-1,  xmax =1,  xticklabels={#1},   box plot width=5mm]}

so that

\optaxis

is xticklabels={}

\optaxis[test1]

is xticklabels={test1}

\optaxis[test1,test2]

is xticklabels={test1,test2}

Or perhaps no optional argument at all so

\newcommand{\optaxis}[1]{%%%
 \begin{axis} [xtick={0},   xmin=-1,  xmax =1,  xticklabels={#1},   box plot width=5mm]}

so that

\optaxis{test1}

is xticklabels={test1}

\optaxis{test1,test2}

is xticklabels={test1,test2}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .