Automatic Theorem Numbering

Can now refer to numbered theorems by \ref{...}, as in LaTeX
This commit is contained in:
Jacques Distler 2008-10-20 00:24:22 -05:00
parent da81a2fbdb
commit 2fb41f12ce
7 changed files with 72 additions and 14 deletions

View file

@ -181,7 +181,6 @@
\newcommand{\quadrupleintegral}{\iiiint}
\newcommand{\conint}{\oint}
\newcommand{\contourintegral}{\oint}
\newcommand{\qed}{\blacksquare}
\newcommand{\infinity}{\infty}
\renewcommand{\empty}{\emptyset}
\newcommand{\bottom}{\bot}
@ -212,9 +211,9 @@
\newtheorem*{uprop}{Proposition}
\newtheorem*{ucor}{Corollary}
\theoremstyle{definition}
\newtheorem{def}{Definition}
\newtheorem{defn}{Definition}
\newtheorem{example}{Example}
\newtheorem*{udef}{Definition}
\newtheorem*{udefn}{Definition}
\newtheorem*{uexample}{Example}
\theoremstyle{remark}
\newtheorem{remark}{Remark}

View file

@ -17,12 +17,13 @@ table.plaintable {
margin-left:30px;
}
.noborder td, .noborder th {border:0}
body {counter-reset: theorem lemma proposition corollary example remark}
body {counter-reset: theorem lemma proposition corollary definition example remark note}
.un_theorem *, .num_theorem *,
.un_lemma *, .num_lemma *,
.un_prop *, .num_prop *,
.un_cor *, .num_cor * {font-style: italic}
span.theorem_label {font-style:normal; font-weight:bold;}
.proof span.theorem_label {font-style:italic;}
.num_theorem .theorem_label:after {
content: " " counter(theorem); counter-increment: theorem;}
.num_lemma .theorem_label:after {
@ -31,7 +32,11 @@ span.theorem_label {font-style:normal; font-weight:bold;}
content: " " counter(proposition); counter-increment: proposition;}
.num_cor .theorem_label:after {
content: " " counter(corollary); counter-increment: corollary;}
.num_defn .theorem_label:after {
content: " " counter(definition); counter-increment: definition;}
.num_example .theorem_label:after {
content: " " counter(example); counter-increment: example;}
.num_remark .theorem_label:after {
content: " " counter(remark); counter-increment: remark;}
.num_note .theorem_label:after {
content: " " counter(note); counter-increment: note;}

View file

@ -393,12 +393,13 @@ span.keyboard {
text-align:center;
}
body {counter-reset: theorem lemma proposition corollary example remark}
body {counter-reset: theorem lemma proposition corollary definition example remark note}
.un_theorem *, .num_theorem *,
.un_lemma *, .num_lemma *,
.un_prop *, .num_prop *,
.un_cor *, .num_cor * {font-style: italic}
span.theorem_label {font-style:normal; font-weight:bold;}
.proof span.theorem_label {font-style:italic;}
.num_theorem .theorem_label:after {
content: " " counter(theorem); counter-increment: theorem;}
.num_lemma .theorem_label:after {
@ -407,8 +408,12 @@ span.theorem_label {font-style:normal; font-weight:bold;}
content: " " counter(proposition); counter-increment: proposition;}
.num_cor .theorem_label:after {
content: " " counter(corollary); counter-increment: corollary;}
.num_defn .theorem_label:after {
content: " " counter(definition); counter-increment: definition;}
.num_example .theorem_label:after {
content: " " counter(example); counter-increment: example;}
.num_remark .theorem_label:after {
content: " " counter(remark); counter-increment: remark;}
.num_note .theorem_label:after {
content: " " counter(note); counter-increment: note;}

View file

@ -904,7 +904,6 @@ class WikiControllerTest < Test::Unit::TestCase
\newcommand{\quadrupleintegral}{\iiiint}
\newcommand{\conint}{\oint}
\newcommand{\contourintegral}{\oint}
\newcommand{\qed}{\blacksquare}
\newcommand{\infinity}{\infty}
\renewcommand{\empty}{\emptyset}
\newcommand{\bottom}{\bot}
@ -935,9 +934,9 @@ class WikiControllerTest < Test::Unit::TestCase
\newtheorem*{uprop}{Proposition}
\newtheorem*{ucor}{Corollary}
\theoremstyle{definition}
\newtheorem{def}{Definition}
\newtheorem{defn}{Definition}
\newtheorem{example}{Example}
\newtheorem*{udef}{Definition}
\newtheorem*{udefn}{Definition}
\newtheorem*{uexample}{Example}
\theoremstyle{remark}
\newtheorem{remark}{Remark}

View file

@ -70,9 +70,8 @@ MaRuKu::In::Markdown::register_block_extension(
if al_string =~ /^\{(.*)\}\s*$/
inside = $1
cs = MaRuKu::In::Markdown::SpanLevelParser::CharSource
# al = al_string &&
# doc.read_attribute_list(cs.new(inside), its_context=nil, break_on=[nil])
al = doc.read_attribute_list(cs.new(inside), its_context=nil, break_on=[nil])
al = al_string &&
doc.read_attribute_list(cs.new(inside), its_context=nil, break_on=[nil])
end
src = MaRuKu::In::Markdown::BlockLevelParser::LineSource.new(lines)
@ -85,8 +84,28 @@ MaRuKu::In::Markdown::register_block_extension(
module MaRuKu; class MDElement
def md_div(children, a=nil)
self.md_el(:div, children, meta={}, a)
def md_div(children, al=nil)
type = label = num = nil
doc.refid2ref ||= {}
if al
al.each do |k, v|
case k
when :class
type = $1 if v =~ /^num_(\w*)/
when :id
label = v
end
end
end
if type
doc.refid2ref[type] ||= {}
num = doc.refid2ref[type].length + 1 || 1
end
e = self.md_el(:div, children, meta={:label => label, :type => type, :num => num}, al)
if type && label
doc.refid2ref[type].update({label => e})
end
e
end
end end

View file

@ -2,7 +2,7 @@ module MaRuKu
class MDDocument
# Hash equation id (String) to equation element (MDElement)
attr_accessor :eqid2eq
attr_accessor :eqid2eq, :refid2ref
def is_math_enabled?
get_setting :math_enabled
@ -103,3 +103,17 @@ end
true
}
)
# This adds support for \ref
RegRef = /\\ref\{(\w*)\}/
MaRuKu::In::Markdown::register_span_extension(
:chars => [?\\, ?(],
:regexp => RegRef,
:handler => lambda { |doc, src, con|
return false if not doc.is_math_enabled?
refid = src.read_regexp(RegRef).captures.compact.first
r = doc.md_el(:divref, [], meta={:refid=>refid})
con.push r
true
}
)

View file

@ -165,6 +165,23 @@ module MaRuKu; module Out; module HTML
end
end
def to_html_divref
ref= nil
self.doc.refid2ref.each_value { |h|
ref = h[self.refid] if h.has_key?(self.refid)
}
if ref
num = ref.num
a = Element.new 'a'
a.attributes['class'] = 'maruku-ref'
a.attributes['href'] = "#" + self.refid
a << Text.new(num.to_s)
a
else
maruku_error "Cannot find div #{self.refid.inspect}"
Text.new "\\ref{#{self.refid}}"
end
end
end end end