Guenter Milde wrote:
On 2009-05-18, Helge Hafting wrote:
[email protected] wrote:
On Friday 15 May 2009, [email protected] wrote:

I want to have a counter (experiment) that uses roman numerals and
is sequential throughout the chapters. I figure that a module is the
right way about this, so I have a module with the following counter:


Yes. You have to use refstepcounter inside your \experimentSection command. This increments the counter for you, and also sets the reference string so that the next label will get the experiment number instead of the section number. Note that \refstepcounter won't work
inside a \section command though. (latex limitation)

The whole module looks like this:

...

It can be slightly simplified:

#\DeclareLyXModule{testexperiment}
#This is a test!

AddToPreamble
        \newcounter{experiment}\setcounter{experiment}{1}
        \renewcommand{\theexperiment}{\textit{\Roman{experiment}}}
EndPreamble

## A new section style, for the correct printing of the experiment title that also looks nice on the screen:
Style Experiment
        CopyStyle             Section
        LatexName             experimentSection
        LabelType             Counter
        LabelString           "\thesection  Experiment\theexperiment."
        LabelCounter          section
        OptionalArgs          1
        Font
                Shape   Italic
                Size    Large
        EndFont
         Preamble
          \newcommand{\experimentSection}[1]{%
                \section{\textit{Experiment \theexperiment. #1}}
                \refstepcounter{experiment}
          }
        EndPreamble
End


And we also have to use \stepcounter _before_ the section, so that the printed number won't be one too low.
...
I know of no way to set the reference string _without_ also incrementing the counter. Such a command would make things simpler, but I don't think it exists.

I just start the counter wiht 1 instead of 0, so it is correct
*before* \refstepcounter is applied.

I tried that during my testing, but the problem is that references to
any label inside such a "experiment" will be off by one!

"See experiment 8" when the label is in "experiment 7" is not useful. Everything will seem fine as long as you don't use cross-references.

Helge Hafting

Reply via email to