On 27.07.08, Steve Litt wrote: > On Sunday 27 July 2008 17:09, Steve Litt wrote: > > ... > > if I create and use two LyX styles, Code1 and Code2, each of them > > with CopyStyle LyX-Code, then on conversion to PDF I get the following > > error message: > > > > LaXeX Error: Command \lyxcode already defined > > > > I can use all Code1, or all Code2, or all LyX-Code, but if I have any two > > of them in the document, I get an error like described above. ... > > Is there a simple way I can use CopyStyle with LyX-Code and not get this > > error?
Yes. Overwrite the Preamble definiton by defining a replacement (maybe empty) in your copy. > I found a fix I can use locally, and a fix that could be made with the LyX > distribution. Let me start with the latter... > LyX-Code is defined in lyxdir/layouts/lyxmacros.inc thusly: > Style LyX-Code ... > Preamble ... > EndPreamble ... > End > Notice that the LaTeX environment, lyxcode, is defined within the LyX > environment definition, so if LyX-Code is mentioned twice, \lyxcode is > defined twice, which is an error. No. If you use LyX-Code n times (with n = 1, ..., \infty) in your document, exactly one Preamble code is inserted in the latex preamble. Iy you do not use LyX-Code (i.e. n=0), the Preamble latex code is not used. > To fix the problem, the developers could remove everything from Preamble to > EndPreamble in the LyX-Code definition, and put it after the "Format 4" and > before any LyX style definitions. In this case, every document that used this layout will have the \lyxcode definition in the latex preamble. As most documents do not use LyX-Code, this is not desired. > It's nice and modular to put the LaTeX definition inside the LyX > definition, but doing so greatly reduces the value of CopyStyle, so I'd > recommend against it. It also produces cleaner LaTeX sources, so I want it to stay. All you have to remember is that the Styles that define their own preamble need special care when copied with CopyStyle (i.e. a new Preamble definition). > In other words, I copied and pasted \lyxcode to \litt_lyxcode within my > layout file, and copied and pasted LyX-Code (minus the > Preamble/EndPreamble section) to litt_LyX-Code within my layout file, > and then did CopyStyle on litt_LyX-Code. It worked perfectly! If you need the lyxcode definition in several Styles, you can: 1. Copy the Preamble section from the original LyXCode to your layout file (as general Preamble code). 2. Redefine LyXCode: Style LyXCode Preamble EndPreamble 3. Copy the redefined one, e.g. Style MyLyXCode CopyStyle LyXCode LatexName \mycode Preamble \newcommand{\mycode}{Hi world!\\ \lyxcode} EndPreamble End This is Untested, but you should get the general idea! Günter