Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
849 views
in Technique[技术] by (71.8m points)

conditional statements - LaTeX ewcommand default argument: is empty?

I'm trying to write a simple example command that prints nothing without an argument, but with an argument it surrounds it with something.

I've read that the default value should be @empty and the simple ifx@empty#1 condition should do the job:


ewcommand{optarg}[1][@empty]{%
ifx@empty#1  {}  else  {(((#1)))}  fi
}

optarg % (((empty)))
optarg{} % (((empty)))
optarg{test} % (((empty))) test

The latter three commands all print the empty word for some reason, and I want the first two to print nothing and the last to print (((test))).

I'm using TeXLive/Ubuntu. An ideas?

question from:https://stackoverflow.com/questions/2144176/latex-newcommand-default-argument-is-empty

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try the following test:

documentclass{article}

usepackage{xifthen}% provides isempty test


ewcommand{optarg}[1][]{%
  ifthenelse{isempty{#1}}%
    {}% if #1 is empty
    {(((#1)))}% if #1 is not empty
}

egin{document}

Testing verb|optarg|: optarg% prints nothing

Testing verb|optarg[]|: optarg[]% prints nothing

Testing verb|optarg[test]|: optarg[test]% prints (((test)))

end{document}

The xifthen package provides the ifthenelse construct and the isempty test.

Another option is to use the ifmtarg package (see the ifmtarg.sty file for the documentation).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...