Template:Italics correction/calc

From blackwiki
< Template:Italics correction
Revision as of 13:29, 17 November 2016 by 71.41.210.146 (talk) (Incomplete, but the outline is there. Saving before I do something stupid and lose this.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 and A 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 Template:Mvar Template:Mvar r′ R′ r R Template:Mvar Template:Mvar κ′ Κ′ κ Κ Template:Mvar Template:Mvar
b′ B′ b B Template:Mvar Template:Mvar s′ S′ s S Template:Mvar Template:Mvar λ′ Λ′ λ Λ Template:Mvar Template:Mvar
c′ C′ c C Template:Mvar Template:Mvar t′ T′ t T Template:Mvar Template:Mvar μ′ Μ′ μ Μ Template:Mvar Template:Mvar
d′ D′ d D Template:Mvar Template:Mvar u′ U′ u U Template:Mvar Template:Mvar ν′ Ν′ ν Ν Template:Mvar Template:Mvar
e′ E′ e E Template:Mvar Template:Mvar v′ V′ v V Template:Mvar Template:Mvar ξ′ Ξ′ ξ Ξ Template:Mvar Template:Mvar
f′ F′ f F Template:Mvar Template:Mvar w′ W′ w W Template:Mvar Template:Mvar ο′ Ο′ ο Ο Template:Mvar Template:Mvar
g′ G′ g G Template:Mvar Template:Mvar x′ X′ x X Template:Mvar Template:Mvar π′ Π′ π Π Template:Mvar Template:Mvar
h′ H′ h H Template:Mvar Template:Mvar y′ Y′ y Y Template:Mvar Template:Mvar ρ′ Ρ′ ρ Ρ Template:Mvar Template:Mvar
Implementation
The characters are grouped together based on the amount of space required. Then, using the #switch parser function, this steps through a number of {{#invoke:String|find}} instances which look up the single-letter parameter in each of the groups.
Since {{#invoke:String|find}} returns 0 if the argument is not found, and a non-zero string position if it is, the return value is inverted with {{#expr:not}} and then compared to 0 to indicate a match.
For efficiency, the cases in the {{#switch}} should be sorted in decreasing order of likelihood.
This could be written as one giant 100-way {{#switch}}, branching on the passed-in character, but I'm guessing (verify this!) that {{#invoke:String|find}} is faster than the general parser.