Discussion:
Macros with optional arguments - LyX 1.6.1
James C. Sutherland
2009-01-06 15:58:17 UTC
Permalink
I have been using macros in 1.6.1, and recently tried to use a macro
with an optional argument. I have attached a trivial LyX file that
shows this macro and an example of its use. This compiles through LyX
just fine.

The problem is that if I export the file to LaTeX and then try to
compile it, the macro definition seems to create problems. This does
not occur with macros that only have required arguments. I don't know
much about LaTeX macros, so I am not sure if what LyX is doing for the
LaTeX export makes sense or not. However, macros with optional
arguments have the "\newcommandx" tag while macros without optional
arguments do not. The "\newcommandx" tag seems to give LaTeX some
trouble. Specifically, when I compile the .tex file, I get the error
"Undefined control sequence" when the "\newcommandx" line is parsed.
I have also attached the exported .tex file.

Can anyone duplicate this? Is this a known problem?

I am on Mac OSX with LyX 1.6.1.

James
James C. Sutherland
2009-01-06 16:08:04 UTC
Permalink
Okay, I think that I found the problem: I am missing the "xargs"
package. The exported LaTeX file actually had a line:
\usepackage{xargs}[2008/03/08]
which I discarded. This is presumably where the "\newcommandx" tag is
defined. The problem is that when I run LaTeX on the file, I get an
error:
"File 'xargs.sty' not found"
The LyX file compiles fine, so I must have this file installed, but I
cannot seem to find where. I have attached the actual .tex file that
LyX produces. Is anyone else able to compile this?

James
Jean-Marc Lasgouttes
2009-01-07 09:59:55 UTC
Permalink
[cc:ing to lyx-devel]
Post by James C. Sutherland
Okay, I think that I found the problem: I am missing the "xargs"
\usepackage{xargs}[2008/03/08]
which I discarded. This is presumably where the "\newcommandx" tag is
defined. The problem is that when I run LaTeX on the file, I get an
"File 'xargs.sty' not found"
The LyX file compiles fine, so I must have this file installed, but I
cannot seem to find where. I have attached the actual .tex file that
LyX produces. Is anyone else able to compile this?
I think it was an error to rely on such a new package to provide this
functionality. The example document is clearly a case that can be
handled by a plain \newcommand statement.

Could LyX be smarter in the kind of code it exports and only use weird
packages when really needed?

Stefan, are you here?

JMarc
Stefan Schimanski
2009-01-07 21:03:02 UTC
Permalink
Post by Jean-Marc Lasgouttes
[cc:ing to lyx-devel]
Post by James C. Sutherland
Okay, I think that I found the problem: I am missing the "xargs"
\usepackage{xargs}[2008/03/08]
which I discarded. This is presumably where the "\newcommandx" tag is
defined. The problem is that when I run LaTeX on the file, I get an
"File 'xargs.sty' not found"
The LyX file compiles fine, so I must have this file installed, but I
cannot seem to find where. I have attached the actual .tex file that
LyX produces. Is anyone else able to compile this?
I think it was an error to rely on such a new package to provide this
functionality. The example document is clearly a case that can be
handled by a plain \newcommand statement.
Could LyX be smarter in the kind of code it exports and only use weird
packages when really needed?
In fact it is supposed to be smart enough.

xargs is only needed if you have macros with optional arguments. Of
course, \newcommand could handle the case with one optional. But, what
we need in fact is something equivalent to \global\def for this case.
Is there a way to do this with \newcommand?

Stefan
Jean-Marc Lasgouttes
2009-01-07 21:45:00 UTC
Permalink
Post by Stefan Schimanski
xargs is only needed if you have macros with optional arguments. Of
course, \newcommand could handle the case with one optional. But,
what we need in fact is something equivalent to \global\def for
this case. Is there a way to do this with \newcommand?
Why \global?

I think that \global\newcommand works in any case.

It would be nice to make the case "all optional arguments are before
required ones" work with plain \newcommand.

JMarc
Stefan Schimanski
2009-01-08 17:43:47 UTC
Permalink
Post by Jean-Marc Lasgouttes
Post by Stefan Schimanski
xargs is only needed if you have macros with optional arguments. Of
course, \newcommand could handle the case with one optional. But,
what we need in fact is something equivalent to \global\def for
this case. Is there a way to do this with \newcommand?
Why \global?
I think that \global\newcommand works in any case.
It does? I think I tried and it did not.

A second requirement: the distinction between \newcommand and
\renewcommand is very fragile. It is far from trivial to say whether a
macro is redefined in some cases. E.g., in a lyx comment a definition
will be ignored.
Post by Jean-Marc Lasgouttes
It would be nice to make the case "all optional arguments are before
required ones" work with plain \newcommand.
What do you mean? I had a self-made \newcommand implementation that
could do that. But it is quite large (obviously), and it also look
strange to put that into the preamble. For that reason we switched to
xargs instead.

Stefan
Jean-Marc Lasgouttes
2009-01-08 22:41:11 UTC
Permalink
Post by Stefan Schimanski
Post by Jean-Marc Lasgouttes
I think that \global\newcommand works in any case.
It does? I think I tried and it did not.
I might be wrong. I cannot find reference to that right now.
Post by Stefan Schimanski
A second requirement: the distinction between \newcommand and
\renewcommand is very fragile. It is far from trivial to say whether a
macro is redefined in some cases. E.g., in a lyx comment a definition
will be ignored.
LyX should skip macro definitions in comments anyway.
Post by Stefan Schimanski
Post by Jean-Marc Lasgouttes
It would be nice to make the case "all optional arguments are before
required ones" work with plain \newcommand.
What do you mean? I had a self-made \newcommand implementation that
could do that. But it is quite large (obviously), and it also look
strange to put that into the preamble. For that reason we switched to
xargs instead.
Sorry, I was mistaken about what newcommand can do. I meant: "commands
with one optional argument before the required ones".

JMarc

Stefan Schimanski
2009-01-08 17:55:04 UTC
Permalink
Post by Jean-Marc Lasgouttes
Post by Stefan Schimanski
xargs is only needed if you have macros with optional arguments. Of
course, \newcommand could handle the case with one optional. But,
what we need in fact is something equivalent to \global\def for
this case. Is there a way to do this with \newcommand?
Why \global?
Because without the logic is complicated:

\newcommand{\foo}{a} \textbf{\renewcommand{\foo}{b} bla} $\foo$

What is printed in the output? What should be shown on screen?

Stefan
Stefan Schimanski
2009-01-07 21:05:00 UTC
Permalink
Post by James C. Sutherland
I have been using macros in 1.6.1, and recently tried to use a macro
with an optional argument. I have attached a trivial LyX file that
shows this macro and an example of its use. This compiles through
LyX just fine.
The problem is that if I export the file to LaTeX and then try to
compile it, the macro definition seems to create problems. This
does not occur with macros that only have required arguments. I
don't know much about LaTeX macros, so I am not sure if what LyX is
doing for the LaTeX export makes sense or not. However, macros with
optional arguments have the "\newcommandx" tag while macros without
optional arguments do not. The "\newcommandx" tag seems to give
LaTeX some trouble. Specifically, when I compile the .tex file, I
get the error "Undefined control sequence" when the "\newcommandx"
line is parsed. I have also attached the exported .tex file.
Can anyone duplicate this? Is this a known problem?
I am on Mac OSX with LyX 1.6.1.
Updating your MacTex to the latest version will solve the problem.

Stefan
James C. Sutherland
2009-01-07 22:31:23 UTC
Permalink
Post by Stefan Schimanski
Updating your MacTex to the latest version will solve the problem.
Updating MacTex did the job. Thank you!

James
Loading...