Template:Italics correction/calc

From Vigyanwiki
< Template:Italics correction

0.1em

Template documentation

This template takes one parameter, a single letter, and returns an amount of space (in units of "em", so something like "0.1em") that should be added after an italic version of that letter to prevent tall characters from colliding with its top-right corner. It is large for letters like f, l and T, and zero for letters like o, A and L which do not protrude to the top right.

Examples:

  • {{Template:Italics correction/calc|f}} = 0.15em
  • {{Template:Italics correction/calc|j}} = 0.1em
  • {{Template:Italics correction/calc|s}} = 0.05em
  • {{Template:Italics correction/calc|h}} = 0em

Because it does not know what font is in use on the user's browser, it is a much cruder approximation than TeX's "italic correction" macro \/, but it's still better than a fixed space.

The primary application is mathematics, where italic letters are often followed by a close parenthesis, superscript, or prime symbol. Thus, the Latin and Greek alphabets are covered, but not the entire Unicode space. Some Hebrew and Cyrillic characters could be added as required.

Full list of characters
Italic text {{mvar}} Italic text {{mvar}} Italic text {{mvar}}
Bare +pad Bare +pad Bare +pad Bare +pad Bare +pad Bare +pad
a′ A′ a A a′ A′ a A r′ R′ r R r′ R′ r R ι′ Ι′ ι Ι ι′ Ι′ ι Ι
b′ B′ b B b′ B′ b B s′ S′ s S s′ S′ s S κ′ Κ′ κ Κ κ′ Κ′ κ Κ
c′ C′ c C c′ C′ c C t′ T′ t T t′ T′ t T λ′ Λ′ λ Λ λ′ Λ′ λ Λ
∂′d′ D′ d D ∂′d′ D′ d D u′ U′ u U u′ U′ u U μ′ Μ′ μ Μ μ′ Μ′ μ Μ
e′ E′ e E e′ E′ e E v′ V′ v V v′ V′ v V ν′ Ν′ ν Ν ν′ Ν′ ν Ν
f′ F′ f F f′ F′ f F w′ W′ w W w′ W′ w W ξ′ Ξ′ ξ Ξ ξ′ Ξ′ ξ Ξ
g′ G′ g G g′ G′ g G x′ X′ x X x′ X′ x X ο′ Ο′ ο Ο ο′ Ο′ ο Ο
h′ H′ h H h′ H′ h H y′ Y′ y Y y′ Y′ y Y ϖ′π′ Π′ ϖπ Π ϖ′π′ Π′ ϖ′π Π
i′ I′ i I i′ I′ i I z′ Z′ z Z z′ Z′ z Z ϱ′ρ′ Ρ′ ϱρ Ρ ϱ′ρ′ Ρ′ ϱρ Ρ
j′ J′ j J j′ J′ j J α′ Α′ α Α α′ Α′ α Α ς′σ′ Σ′ ςσ Σ ς′σ′ Σ′ ςσ Σ
k′ K′ k K k′ K′ k K β′ Β′ β Β β′ Β′ β Β τ′ Τ′ τ Τ τ′ Τ′ τ Τ
l′ℓ′ L′ l L l′ℓ′ L′ l L γ′ Γ′ γ Γ γ′ Γ′ γ Γ υ′ Υ′ϒ′ υ Υϒ υ′ Υ′ϒ′ υ Υϒ
m′ M′ m M m′ M′ m M δ′ Δ′ δ Δ δ′ Δ′ δ Δ φ′ϕ′ Φ′ φϕ Φ φ′ϕ′ Φ′ φϕ Φ
n′ N′ n N n′ N′ n N ε′ϵ′ Ε′ εϵ Ε ε′ϵ′ Ε′ εϵ Ε χ′ Χ′ χ Χ χ′ Χ′ χ Χ
o′ O′ o O o′ O′ o O ζ′ Ζ′ ζ Ζ ζ′ Ζ′ ζ Ζ ψ′ Ψ′ ψ Ψ ψ′ Ψ′ ψ Ψ
p′ P′ p P p′ P′ p P η′ Η′ η Η η′ Η′ η Η ω′ Ω′ ω Ω ω′ Ω′ ω Ω
q′ Q′ q Q q′ Q′ q Q ϑ′θ′ Θ′ ϑθ Θ ϑ′θ′ Θ′ ϑθ Θ
Implementation
It's just a big 109-way #switch statement. For the sake of efficiency (this is likely to be used many times on a page), letters which take the same amount of space are grouped together, and the groups are sorted so the most common ones come first.
I tried
{{#switch:0
|{{#expr:not{{#invoke:String|find|dijkrtvwxyzCDEFGHIJKMNUVWXYZγδζξϖπστΕΖΗΙΚΜΝΞΠΣΥϒΧΨ|{{{1}}}}}}}=0.1em
|{{#expr:not{{#invoke:String|find|cgsuBGOPQRSαβϑθκμνςυχψωΒΘΟΡΩ|{{{1}}}}}}}=0.05em
|{{#expr:not{{#invoke:String|find|ab∂ehmnopqALεϵηιλοϱρφϕΑΔΛΦ|{{{1}}}}}}}=0em
|{{#expr:not{{#invoke:String|find|flTΓΤ|{{{1}}}}}}}=0.15em
|0=0.1em
}}

to reduce the number of cases in the #switch, but that was clearly slower. (The large table makes this documentation page a good benchmark!)

One significant limitation of this template is that it does not accept characters in HTML entity form. While it could be extended to accept |&alpha; or |&#945; or |&#x3B1;, adding all those forms would really bloat the #switch.

[[Category:Template documentation pages{{#translation:}}]]