Hello,
the attached patch provides an additional entry for the GuiRef reference
insertion dialog which is supposed to automatically provide the correct
"format" for the reference, specifically:
1. if the label is inside a footnote, then the reference has a footnote
ref format,
i.e., it resembles $^(<number>)$.
2. if the label is inside an equation, then the ref is formatted with
\eqref{}, i.e.,
"(<number>)".
3. otherwise, it is a normal \ref{}
Unfortunately, the equation case doesn't work yet, because I couldn't
figure out
what is exactly linked in the TOC (not the math inset in which the label
is inserted ?)
Also, note that how it works is only visible in the "View Source" panel
(you can check
View Entire Source, then copy the whole thing and paste into an external
.tex file
and compile it), but not yet if you type "Ctrl+t" or "Export->LaTeX
(normal)" 'cause
in such cases the TOC is not found (why ?!?).
There was some e-mail discussion about backward compatibility issues on
this thread
http://www.mail-archive.com/[email protected]/msg166112.html
so I came up with the attached preliminary patch, which doesn't change
anything for
already existing lyx documents (I hope). If you like the idea and
approach, I might
finalize it.
I'll take the chance for notifying a few inconsistencies of the GUI for
the "xref subsystem":
-) The second entry in the typeCO combo of the dialog says
"(<reference>)", so the
non-latex-expert user may be tempted to think that it's only a
matter of taste and
appearance, and select this for a normal label, but in such a case
the generated
\eqref{} command doesn't work and a "(??)" is generated in the output.
I would suggest to rework that label as "Equation Reference"
instead (but it should
actually be forbidden for non-equation refs). This should be
reported in the Trac,
if not already there.
-) There are some inconsistencies among the text shown to the user in
that dialog
as "reference format" (i.e., the entries in the RefUi.ui), and the
short text that is
shown as prefix in the on-screen inset (i.e., the text coming from
InsetRef.cpp).
This is the whole list:
"<reference>" => "Ref: label"
"(<reference>)" => "EqRef: label"
"Page" => "Page: label"
"On page" => "TextPage: label"
"<reference> on page <page>" => "Ref+Text:label"
"Formatted reference" => "Format: label"
"Textual reference" => "NameRef:label" [ this is missing the
extra-space w.r.t. others]
Also, the shown colon ":" is somewhat ugly, due to the usual
convention in using
normally colons within label texts themselves, i.e.:
"EqRef:eq:foo". I would call
for nicer ideas on what to show on the screen inside this inset,
i.e., something
more consistent with what is shown on the selection dialog.
-) Finally, some missing options might be the ones that one finds
normally in OpenOffice,
i.e., how to reference:
-) the section or chapter within which the label is contained
-) the label number, with or without "full context" (including
chapters/sections).
Any comment on the patch itself or the above issues is welcome.
Bye,
Tommaso
Index: src/insets/InsetRef.cpp
===================================================================
--- src/insets/InsetRef.cpp (revisione 37863)
+++ src/insets/InsetRef.cpp (copia locale)
@@ -55,7 +55,8 @@
|| s == "vpageref"
|| s == "formatted"
|| s == "eqref"
- || s == "nameref";
+ || s == "nameref"
+ || s == "autoformat";
}
@@ -136,10 +137,52 @@
}
+static bool labelInFootnote(Buffer const & buf, docstring const & label) {
+ Toc & toc = buf.tocBackend().toc("label");
+ Toc::iterator it = toc.item(0, label);
+ if (it == toc.end()) {
+ LYXERR(Debug::DEBUG, "No TOC");
+ return false;
+ }
+ TocItem & t = *toc.item(0, label);
+ DocIterator dit = t.dit();
+ return dit.inset().lyxCode() == FOOT_CODE;
+}
+
+
+static bool labelInEquation(Buffer const & buf, docstring const & label) {
+ Toc & toc = buf.tocBackend().toc("label");
+ Toc::iterator it = toc.item(0, label);
+ if (it == toc.end()) {
+ LYXERR(Debug::DEBUG, "No TOC");
+ return false;
+ }
+ TocItem & t = *toc.item(0, label);
+ DocIterator dit = t.dit();
+ return dit.inset().lyxCode() == MATH_CODE
+ || dit.inset().lyxCode() == MATH_ENV_CODE
+ || dit.inset().lyxCode() == MATH_ENSUREMATH_CODE
+ || dit.inset().lyxCode() == MATH_CODE;
+}
+
+
void InsetRef::latex(otexstream & os, OutputParams const & rp) const
{
string const cmd = getCmdName();
- if (cmd != "formatted") {
+
+ if (cmd == "autoformat") {
+ docstring const ref = getParam("reference");
+ LYXERR(Debug::DEBUG, "ref=" << ref
+ << ", inFn()=" << labelInFootnote(buffer(), ref)
+ << ", inEq()=" << labelInEquation(buffer(), ref));
+ if (labelInFootnote(buffer(), ref))
+ os << "\\reffnmark{" << ref << "}";
+ else if (labelInEquation(buffer(), ref))
+ os << "\\eqref{" << ref << "}";
+ else
+ os << "\\ref{" << ref << "}";
+ return;
+ } else if (cmd != "formatted") {
// We don't want to output p_["name"], since that is only used
// in docbook. So we construct new params, without it, and use that.
InsetCommandParams p(REF_CODE, cmd);
@@ -147,8 +190,8 @@
p["reference"] = ref;
os << p.getCommand(rp);
return;
- }
-
+ }
+
// so we're doing a formatted reference.
docstring const data = getEscapedLabel(rp);
docstring label;
@@ -323,6 +366,16 @@
features.require("amsmath");
else if (cmd == "nameref")
features.require("nameref");
+ if (cmd == "autoformat" &&
+ labelInFootnote(buffer(), getParam("reference"))) {
+ features.addPreambleSnippet(
+ "\\newcommand{\\reffnmark}[1]{%\n"
+ " \\begingroup\n"
+ " \\unrestored@protected@xdef\\@thefnmark{\\ref{#1}}%\n"
+ " \\endgroup\n"
+ "\\@footnotemark}"
+ );
+ }
}
@@ -334,6 +387,7 @@
{ "vref", N_("Standard+Textual Page"), N_("Ref+Text: ")},
{ "formatted", N_("Formatted"), N_("Format: ")},
{ "nameref", N_("Reference to Name"), N_("NameRef:")},
+ { "autoformat",N_("Autodetect format"), N_("AutoRef:")},
{ "", "", "" }
};
Index: src/frontends/qt4/ui/RefUi.ui
===================================================================
--- src/frontends/qt4/ui/RefUi.ui (revisione 37863)
+++ src/frontends/qt4/ui/RefUi.ui (copia locale)
@@ -324,6 +324,11 @@
<string>Textual reference</string>
</property>
</item>
+ <item>
+ <property name="text" >
+ <string>Autodetect format</string>
+ </property>
+ </item>
</widget>
</item>
<item row="4" column="0" >