[email protected] wrote:
On Friday 15 May 2009, [email protected] wrote:
We're getting close!
On Friday 15 May 2009, Guenter Milde wrote:
On 2009-05-15, [email protected] wrote:
On Friday 15 May 2009, Helge Hafting wrote:
[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:
3.2 <label expt:reading-ability> Experiment I: Effect of reading ability
Counter experimentCounter
LabelString "\Roman{experimentCounter}"
Within ""
End
This sets up the Counter for LyX only, you need to set up a LaTex
counter as well.
Ah! That explains a lot.
So, I need to code this for TWO systems: (1) LyX (shown on the screen, with all
its handy tools); and (2) LaTeX (printed out correctly)!
I have not been able to get the two to happen at the same time. I can get the
correct solution with the following:
## LyX counter, in correct format:
Counter experimentCounter
LabelString "\Roman{experimentCounter}"
Within ""
End
## LaTeX counter, in correct format:
AddToPreamble
\newcounter{experiment}
\setcounter{experiment}{0}
\renewcommand{\theexperiment}{\textit{\Roman{experiment}}}
EndPreamble
## An LyX style, that just allows me to set a named label for the following
Experiment heading:
Style ExperimentLabel
Category Section
LatexType Command
LatexName exptLabel
KeepEmpty 1
LabelType Counter
LeftMargin XLabel_for_experiment_XXX:XX
LabelString "Label for experiment \theexperimentCounter: "
LabelCounter experimentCounter
OptionalArgs 1
TopSep 1.4
Font
Shape Italic
Color red
Size Large
EndFont
Preamble
\newcommand{\exptLabel}[1]{%
\refstepcounter{experiment} #1}
EndPreamble
End
## 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 \theexperimentCounter."
LabelCounter section
OptionalArgs 1
Font
Shape Italic
Size Large
EndFont
Preamble
\newcommand{\experimentSection}[1]{%
\section{\textit{Experiment \theexperiment. #1}}}
EndPreamble
End
=======================
It took me a long time to find the \refstepcounter{} latex command. It's the
only way that I've managed to change the label reference environment so that
the \label{exp:expt-name} takes the experiment counter value and not the
section counter value.
I don't mind that LyX doesn't show the experiment counter value on the screen,
but I do need to be able to use labels referring to the experiment counter. I
tried writing this latex command, but it doesn't work. Anyone know how to write
this function correctly?
\newcommand{\experimentSection}[1]{%
\section{\textit{Experiment \theexperiment.
{\refstepcounter{experiment} #1}}}}
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:
#\DeclareLyXModule{testexperiment}
#This is a test!
Format 11
Counter experimentCounter
LabelString "\Roman{experimentCounter}"
Within ""
End
AddToPreamble
\newcounter{experiment}\setcounter{experiment}{0}
\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]{%
\stepcounter{experiment}
\section{\textit{Experiment \theexperiment. #1}}
\addtocounter{experiment}{-1}
\refstepcounter{experiment}
}
EndPreamble
End
One thing is missing - the experiment number doesn't show inside LyX
itself. This because there can only be one "LabelCounter", and you don't
want to drop the section counter here, as that will disrupt section
numbering throughout the document. But you didn't care about experiment
numbers inisde LyX, so no real problem.
The experiment numbers will be correct in the printed/pdf output, and
the references will work too. :-)
The latex code may seem more complicated than necessary.
I use \stepcounter to increment the counter before outputting
"Experiment \theexperiment"
Later, I subtract one from the counter, and use refstepcounter that
add one back again, as well as updating the reference string.
Why not just use \refstepcounter instead of the first \stepcounter?
The reason is the use of \section. \section also updates the reference
string, with the section number. But you want to reference experiment
numbers, so we have to use \refstepcounter _after_ the \section in order
to fix that. And we also have to use \stepcounter _before_ the section,
so that the printed number won't be one too low. Finally, we are
incrementing the counter twice, therefore it is necessary to also
subtract one to get things right.
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.
Anyway, this module generates the correct output, and
if you insert a label in the text following an experiment line, then any
cross reference to that label will print the roman numeral for the
experiment.
If you put a label inside the experiment line itself, then cross
references to that internal label will print the experiment's section
number. So be careful how you place your labels!
I.e. it is possible to reference both experiment numbers and experiment
section numbers. The code is hairier than usual, because two counters
gets incremented on the same line.
Helge Hafting