Difference between revisions of "Template:Italics correction/calc"
(Add a few more Greek letter variants that are used as technical symbols, and one Latin (∂). Fill in Greek spacing.) |
(Upon benchmarking, a large #switch turns out to be faster. And add ℓ.) |
||
Line 1: | Line 1: | ||
− | {{#switch: | + | {{#switch:{{{1}}} |
− | + | |d|i|j|k|ℓ|r|t|v|w|x|y|z|C|D|E|F|G|H|I|J|K|M|N|U|V|W|X|Y|Z|γ|δ|ζ|ξ|ϖ|π|σ|τ|Ε|Ζ|Η|Ι|Κ|Μ|Ν|Ξ|Π|Σ|Υ|ϒ|Χ|Ψ=0.1em | |
− | | | + | |c|g|s|u|B|G|O|P|Q|R|S|α|β|ϑ|θ|κ|μ|ν|ς|υ|χ|ψ|ω|Β|Θ|Ο|Ρ|Ω=0.05em |
− | | | + | |a|b|∂|e|h|m|n|o|p|q|A|L|ε|ϵ|η|ι|λ|ο|ϱ|ρ|φ|ϕ|Α|Δ|Λ|Φ=0em |
− | | | + | |f|l|T|Γ|Τ=0.15em |
− | | | + | |0.1em |
}}<noinclude><!-- | }}<noinclude><!-- | ||
+ | Latin lower-case: abc∂defghijlℓmnopqrstuvwxyz | ||
+ | Latin upper-case: ABCDEFGHIJLNOPQRSTUVWXYZ | ||
Greek lower-case: αβγδεϵζηϑθικλμνξοϖπϱρςστυφϕχψω | Greek lower-case: αβγδεϵζηϑθικλμνξοϖπϱρςστυφϕχψω | ||
Greek upper-case: ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥϒΦΧΨΩ | Greek upper-case: ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥϒΦΧΨΩ | ||
+ | When a letter has multiple forms, I'm listing the most common mathematical form last, and other variants (which may or may not be more common in ordinary text) before that. | ||
-->{{documentation|content= | -->{{documentation|content= | ||
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. | 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. | ||
Line 110: | Line 113: | ||
{{!}} {{mvar|τ′ Τ′}} {{!!}} {{mvar|1=τ<span style="padding-left:{{{{ARTICLEPAGENAME}}|τ}}">′</span> Τ<span style="padding-left:{{{{ARTICLEPAGENAME}}|Τ}}">′</span>}} | {{!}} {{mvar|τ′ Τ′}} {{!!}} {{mvar|1=τ<span style="padding-left:{{{{ARTICLEPAGENAME}}|τ}}">′</span> Τ<span style="padding-left:{{{{ARTICLEPAGENAME}}|Τ}}">′</span>}} | ||
{{!}}- | {{!}}- | ||
− | {{!}} '' | + | {{!}} ''l′ℓ′ L′'' {{!!}} ''l<span style="padding-left:{{{{ARTICLEPAGENAME}}|l}}">′</span>ℓ<span style="padding-left:{{{{ARTICLEPAGENAME}}|ℓ}}">′</span> L<span style="padding-left:{{{{ARTICLEPAGENAME}}|L}}">′</span>'' |
− | {{!}} {{mvar| | + | {{!}} {{mvar|l′ℓ′ L′}} {{!!}} {{mvar|1=l<span style="padding-left:{{{{ARTICLEPAGENAME}}|l}}">′</span>ℓ<span style="padding-left:{{{{ARTICLEPAGENAME}}|ℓ}}">′</span> L<span style="padding-left:{{{{ARTICLEPAGENAME}}|L}}">′</span>}} |
{{!}} ''γ′ Γ′'' {{!!}} ''γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|γ}}">′</span> Γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|Γ}}">′</span>'' | {{!}} ''γ′ Γ′'' {{!!}} ''γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|γ}}">′</span> Γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|Γ}}">′</span>'' | ||
{{!}} {{mvar|γ′ Γ′}} {{!!}} {{mvar|1=γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|γ}}">′</span> Γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|Γ}}">′</span>}} | {{!}} {{mvar|γ′ Γ′}} {{!!}} {{mvar|1=γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|γ}}">′</span> Γ<span style="padding-left:{{{{ARTICLEPAGENAME}}|Γ}}">′</span>}} | ||
Line 152: | Line 155: | ||
{{!)}} | {{!)}} | ||
; Implementation | ; Implementation | ||
− | : | + | : It's just a big 109-way [[Help:Switch parser function|<code>#switch</code>]] 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 |
− | : | + | <pre>{{#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 | ||
+ | }}</pre> | ||
+ | to reduce the number of cases in the <code>#switch</code>, but that was clearly slower. (The large table makes this documentation page a good benchmark!) | ||
}}</noinclude> | }}</noinclude> |
Revision as of 15:53, 17 November 2016
0.1em

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.
- 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!)
Editors can experiment in this template's sandbox (create | mirror) and testcases (create) pages. Subpages of this template. |