Template talk:Chem2

Latest comment: 9 months ago by DMacks in topic Staggered Charges

Must preserve minus signs in subscripts

edit

{{chem2|A_{''x''}B_{1−''x''}}} currently produces "AxB1-x" with the minus sign (−) in the subscript replaced by a hyphen (-), in violation of MOS:MINUS. Please make it work properly. — Mikhail Ryazanov (talk) 06:05, 27 February 2023 (UTC)Reply

I'm not familiar with this module, but the following lines of code might be relevant. Perhaps someone knows better? — Martin (MSGJ · talk) 17:35, 27 February 2023 (UTC)Reply
  • 277: formula = formula .. su(x:match("[+-]"):gsub("-", "−"), x:match("%d+"), "");
  • 288: if parameter == "s" then formula = formula .. "−" -- single bond
  • 294: elseif parameter == "-" then formula = formula .. "-" -- -

debugging & tests

edit
see /testcases at § subscript hyphen (Feb 2023)
  • More research:
Test string to be (rule: when final-} in formula input, end pipe }|}} is required):
(A) {{chem2|A_{''x''}H_{1-''x''}|}}AxH1−x
Characters involved:
(H) U+002D - HYPHEN-MINUSAxH1−x
(M) U+2212 MINUS SIGN (−) → AxM1−x
re MSGJ: by reading the code, none of the lines 277 (good replacement), 288 (good replacement), 294 (hyphen only) seem to make the wrong replacement.
-DePiep (talk) 19:02, 27 February 2023 (UTC)Reply
code review:
  • L239 (current live): if not x then x = f:match('^\^{[^}]*}', i); t = T_CARET; end -- ^{...}
^\^{[^}]*} ? expected ^^{[^}]*} (Lua pattern not regex; module preview reports Error: [239:42] invalid escape sequence near '\^'. (Errors from this, missed? hidden?) edited in sandboxDePiep (talk) 19:57, 27 February 2023 (UTC)Reply
  • test code to be:
{{chem2|1=A^{1-''y''}B_{1-''x''}H(2-)}}
Three minus/hyphen signs input, expected result: all three Minus (M). Current live test:
Live M>−<: A1−yB1−xM2−
returns H, H, M. sup/sub issue. -DePiep (talk) 20:43, 27 February 2023 (UTC)Reply

Bug report

edit
(H) U+002D - HYPHEN-MINUS
(M) U+2212 MINUS SIGN (&minus;)

From original report (OP); B2-B (demo/test inline) and (4-) (charge) added as extra testcases (situations 1–4 now).

In the tests here, B2-B is added and is not targeted (no changes claimed nor expected; ignored in conclsions)
{{chem2|1=HA^{''1-x''}B2-B_{3-''x''}(4-)}}
{{chem2|1=HA^{1-x}B2-B_{3-x}(4-)}}
in H>-<: HA1−xB2B3−x4− --  N MINUS expected
in M>−<: MA1−xB2B3−x4− --  N MINUS expected
(A) Original complaint
MINUS (−) not kept when in (topically correct) sub/supscripted formula is entered; shows hyphen (-) instead.
Cause: {{Chem2}} early replaces NDASH and MINUS with HYPHEN, to be treatred similar (see code L250, 251).
When as charge, the hyphen is changed into MINUS all right, but no such replacement in the supsubscripts.
Proposed solution: in T_CARET and T_UNDERSCORE change any hyphen (from early replacement) into MINUS [1].
live, in:H>-<: HA1−xB2B3−x4−
/sbx, in:H>-<: HA1−xB2B3−x4−
live, in:M>−<: MA1−xB2B3−x4−
/sbx, in:M>−<: MA1−xB2B3−x4−
Showing: current live version returns HYPHEN in subsuptexts; correct MINUS inline and in the "(2-)" charge). All irrespective of input character.
(B) Handling of HTML entities (&minus;)
When entering like &minus; (for character MINUS), it is not handled as the character.
Incidentally, this preserves the MINUS character as asked for in the OP (undocumented 'solution', hack).
However, not documented and would give different results between input &minus; vs. . Also, other codes entries could be affected.
Proposed solution: add early decoding mw.text.decode() [2].
  • Entering HTML Entity &minus;
live, in:>&minus;<: EA1−xB2B3−x4− -- incidentally, returns as expected
/sbx, in:>&minus;<: EA1−xB2B3−x4−
Entering &beta;
live, in:>&beta;<: EA1βxB2βBx(4β) -- as expected :/sbx, in:>&beta;<: EA1βxB2βBx(4β) -- same result (returns decoded &beta; to UA: decoded later)
(C) Bad code
L239: if not x then x = f:match('^\^{[^}]*}', i); t = T_CARET; end   -- ^{...}
Parser gives error: '^\^{[^}]*}' has incorrect "\" (note: expected is Lua pattern not Regex). Saves with error (-warning).
No error effects (in pages) known, nor seen in testpage.
From Lua documentation mw:Lua manual: "At other positions, '^' and '$' have no special meaning and represent themselves". That is: the second caret should represent the input character (to indicate superscript).
Proposed solution: Remove the slash: [3]. No effects in tests seen.
Testcases: see superscripts (CARET input) throughout.
(D) Escape codes limitations
The /documentation mentions \s, \- for input "single bond" (topical term) and "hyphen" when used in line. However, they don't work as expected when in sup/subscripted text (as in OP).
Proposed solution: the escape codes are not developed to function more widely. Instead, documentation to be improved (mention limitations).
  • From doc: use \s, \- slashed escape code (note: "B2-B" as expected)
live, in:>\s<: SsA1\sxB2−B3\sx(4−)
/sbx, in:>\s<: SsA1\sxB2−B3\sx(4−)
live, in:>\-<: ShA1\−B2-B3\−x(4-)
/sbx, in:>\-<: ShA1\−B2-B3\−x(4-)
-DePiep (talk) 00:06, 28 February 2023 (UTC)Reply

(T) more tests: bare hyphen: input without no preceding number etc in text element (eg, charge "(-)"):

HA−xBx
HA−xBx
MA−xBx
MA−xBx
EA−xBx -- &minus;
EA−xBx
Adjustmensts, refining testcases, spell uc MINUS etc, create 4 numberred testsituations DePiep 05:41, 28 February 2023
For more bare "-" input tests (not "2-" &tc;), see /testcases (UTC), -DePiep (talk) 06:36, 28 February 2023 (UTC)Reply

Proposal (code edit)

edit
From the Report above, I propose to oconsider: edit all code from module:Chem2/sandbox into module:Chem2 (diff).
Note: the (B) may change behaviour (instances relying on undocumented effects). Conservation action needed? OTOH, when entering &minus;, what else should be expected than MINUS?
Also, I'd like to have chemicist eyes taking a look.
More tests at the Template:Chem2/testcases.
-DePiep (talk) 00:06, 28 February 2023 (UTC)Reply
@Christian75, Mikhail Ryazanov, and MSGJ: demos and tests stable & ok now, please take a look. -DePiep (talk) 06:41, 28 February 2023 (UTC)Reply
Thanks, DePiep! As far as I can tell, the new code works definitely better, at least in terms of indices. I would say that minus signs in indices (upper and lower) are certainly much more common than hyphens, so the general idea to replace input "-" (ASCII hyphen-minus) with output "−" (Unicode minus) is quite reasonable. I suspect that there might be legitimate cases for hyphens in indices when something text-like is used (similar to "_{(g)}", but I can't provide a specific example), so making the escape sequence "\-" producing output "-" in all contexts would be nice. — Mikhail Ryazanov (talk) 22:14, 28 February 2023 (UTC)Reply
@Christian75 and MSGJ:, any remarks? -DePiep (talk) 17:17, 2 March 2023 (UTC)Reply
If you have only a minute to spare, consider checking this essence: in the testases, are the minus-signs and the hyphens in the right places now? DePiep (talk) 18:54, 2 March 2023 (UTC)Reply
  Done * Pppery * it has begun... 22:10, 4 March 2023 (UTC)Reply

Proposal: add parameter checks

edit

I propose to add these parameters checks:

(1) Module:Check for unknown parameters

Checks regular parameter usage, also for unnamed parameters.
|2= can be present, see (2) below, but must be empty.

(2) Check for unbalanced {...} brackets

When entering {{Chem2}} like {{chem2|A_{x}H_{1-x}}}, the final }-brackets are technically mixed up: closing bracket {..} of input and end of template brackets }}.
result: {{chem2|A_{x}H_{1-x}}}AxH_{1x}  N
(B) is to check for such bad parameter effect. The page will be categorised.
  • Solutions (for the editor, /documentation):
add empty pipe: {{chem2|..{..}|}} (empty parameter #2);
add parameters numbers 1 and 2 (empty): {{chem2|1=..{..}}};
add a space: {{chem2|..{..} }}.
  • Both checks will give a preview warning.
Example, by /sandbox2:
{{chem2/sandbox2|foo=H2O|1=A_{x}H_{1-x}}}AxH_{1x}
This warning does not fix or alter the error.
Chem2 code used: Module:Chem2 (=current live code)
Tracking category is Category:Pages using Chem2 with parameter issues (2) (new, one for all parameter checks).
Test code is in {{Chem2/sandbox2}}.
Testcases in Template:Chem2/testcases § Proposal: add parameter checks.

-DePiep (talk) 08:50, 28 February 2023; -DePiep (talk) 13:48, 6 March 2023 (UTC)Reply

demo

edit

Use Preview: All tests are with {{chem2/sandbox2}} (/sandbox2), and the live module.

Check 1, unknown parameter (preview)
A. {{chem2|1=Fe3S2(CO)9|foo=hydro}}Fe3S2(CO)9
Check 2: threee end-brackets = ambiguous & wrong
B. {{chem2|CH4_{(g)}}}CH4_{(g)} (err)
C. {{chem2|CH4_{(g)} |}}CH4(g) (ok)
Check 3: extra pipe is used, but |2= has input
D. {{chem2|CH4_{(g)}|2=barefoo}}CH4(g) -- (|2=barfoo not correct)
E. {{chem2|CH4_{(g)|2=barefoo}}CH4_{(g) -- unbalanced brackets in input string
F. {{chem2|C{1}4_{(g)}}C{1}4_{(g) -- unbalanced brackets in input
G. {{chem2|C1}4_(g)}}C1}4_(g) -- unbalanced closing brackets in input
-DePiep (talk) 16:10, 6 March 2023 (UTC)Reply

Template-protected edit request on 18 March 2023

edit
Notes: only {{Chem2}} template code, module not changed; and ../sandbox2 is used (development reasons).
Changes: (1) parameters check by checkfup,
(2) extra check for end-}-bracket misreading with template-closing brackets }} (See Template:Chem2 § end-bracket issue)
Error category: Category:Pages using Chem2 with parameter issues (2).
Talk/tests/demo: see #Proposal: add parameter checks (preview warnings).
New code only warns/categorises. No changes to template output are made. DePiep (talk) 16:10, 18 March 2023 (UTC)Reply
  Done — Martin (MSGJ · talk) 14:12, 20 March 2023 (UTC)Reply

Template-protected edit request on 20 March 2023

edit

Please replace all code from {{Chem2}} with {{Chem2/sandbox}} (diff).

Change: bugfix. Adds |auto, link= to whitelist for check f.u.p. Was mistakingly missed in edit § Template-protected edit request on 18 March 2023 (by me). Causing Category:Pages using Chem2 with parameter issues (2) to fill. DePiep (talk) 17:05, 20 March 2023 (UTC)Reply

  Completed. P.I. Ellsworth , ed. put'er there 11:18, 21 March 2023 (UTC)Reply
Thanks. Tracking category is populated usefully now. DePiep (talk) 11:46, 21 March 2023 (UTC)Reply
Yes, I saw that, my pleasure! Paine  12:00, 21 March 2023 (UTC)Reply

Roman numeral superscripts to indicate oxidation states

edit

Would it be possible to extend the syntax for charges to superscript Roman numeral oxidation states? It would be convenient (and natural) in an article like Stock nomenclature to type Co(II)Co(III)2O4 and get CoIICoIII2O4. Ben (talk) 23:08, 4 March 2023 (UTC)Reply

Worth exploring indeed. (1) How would the {{Chem2}} input be (in keyboard I', V's etc)?, (2) minuses too? (3) and is there any confusion possibble with charge etc (in that same input)? IOW, what input to expect & to recognise?
Found this: using existing input codes Co^{II}Co^{III}2O4CoIICoIII2O4. Would this do, Ben? DePiep (talk) 11:45, 5 March 2023 (UTC)Reply
Hi DePiep, thanks for finding that code. It works, which is great, so we could just highlight it more prominently alongside the syntax for charges. I don't think there would be any confusion if we made Co(II)Co(III)2O4 possible, though, and it would be easier to remember. I can have a go at implementing it if you'd rather not. Ben (talk) 11:52, 6 March 2023 (UTC)Reply
Go ahead implementing. But by all means: do not use the #2 {{Chem2/sandbox2}}, as it has code in development.
So, there is module:Chem2/sandbox and {{Chem2/sandbox}} (as usual then).
(to consider: check for possible -conflicting/confusing/ambiguous- input; if possible accept lowercase i and v; what with minus?). DePiep (talk) 14:07, 6 March 2023 (UTC)Reply
The problem with Co(II) etc. is that it might be a Roman numeral or it might be some sort of iodine-based subgroup. I'm wary of trying to make the template too clever and have to distinguish between those two cases; the superscript notation ^{II} works fine. User:GKFXtalk 11:32, 13 March 2023 (UTC)Reply
Right so. And "make it not too clever" is a fine reasoning. DePiep (talk) 10:08, 17 March 2023 (UTC)Reply

The {{chem2}} template works incorrectly on certain Wikipedia pages

edit

Why the {{chem2}} template works incorrectly on certain Wikipedia pages? This bug has nothing to do with browsers, operating systems or computer settings, it is a {{chem2}} template bug.

For example, the {{chem2|CO3(2−)}} and {{chem2|NO3−}} normally gives CO2−3 and NO3 respectively, but on certain pages (e.g., Trigonal planar molecular geometry, Caesium sesquioxide and all Search results pages) it crashes and gives CO2−3 and NO−3 respectively. What on this Earth is going on? Can someone correct this annoying bug? Bernardirfan (talk) 13:30, 18 August 2023 (UTC)Reply

Using chem2 inside the wikilink text seems to stuff up subsequent uses with charges over subscripts. Graeme Bartlett (talk) 21:38, 25 October 2023 (UTC)Reply
Even using {{chem2}} outside wikilink and outside anything, the same bug attacks randomly. Seems the {{chem2}} template crashes randomly on certain Wikipedia articles. Sometimes it crashes whenever I change the article outside the {{chem2}} templates, sometimes it crashes only when I press the "Show preview" button, sometimes it crashes both when I press the "Show preview" button and when I press the "Publish changes" button, sometimes it randomly crashes without any reason, just like that. Very wierd bug. Yes, I can use the {{chem}} template and <chem> element instead, but why not correct this annoying {{chem2}} bug in the first place. Bernardirfan (talk) 18:37, 9 December 2023 (UTC)Reply

Incorrect physical state notation

edit

Putting physical state (like solid, gaseous, aqueous) to lower index does not seem to be correct, and examples like

  • CH4(g)

should be fixed to

  • CH4(g)

(See e.g. IUPAC Red Book, IR-2.2.3.1, example 16.)

Mykhal (talk) 10:12, 2 October 2023 (UTC)Reply

As the problem was with doc page, I've already fixed it. —Mykhal (talk) 13:12, 2 October 2023 (UTC)Reply
I agree. I see there are so many incorrect physical state notations accross Wikipedia. As soon as I see such errors, I correct them immediately, according to the IUPAC Red Book. I will search for such errors and try to correct them one by one accros Wikipedia articles, because I like finding errors and correcting them. Dear editors, join us, you can correct them also. Bernardirfan (talk) 15:25, 18 October 2023 (UTC)Reply

Fractions

edit

I'm surprised no one has mentioned this yet, but stoichiometric fractions are a mess with this template. Simply putting in both numbers and a slash results in the following:

3 Me3SiCl + 3 Li + 1/2 N2 → (Me3Si)3N + 3 LiCl

A simple workaround I've seen is to put the numerator as a superscript:

3 Me3SiCl + 3 Li + 1/2 N2 → (Me3Si)3N + 3 LiCl

but this is very ugly IMO, at least compared to {{sfrac}}, 1/2, or its cousin {{frac}}, 12. Putting those templates into the chem2 template produces the following gnarly error:

3 Me3SiCl + 3 Li + '"`UNIQtemplatestyles00000093QINU`"'1<span class="sronly">/2 N2 → (Me3Si)3N + 3 LiCl

Sure, one could split the template up and include {{sfrac}} midway, but then you'd have to encase the whole thing in {{nowrap}} to ensure the equation doesn't line break when the webpage is made thin. Surely there's a way to code for this in the template directly? <chem> detects syntax like '1/2' and automatically renders it as a vertical fraction. User:Graeme Bartlett has told me off before for converting line equations to <chem>, but at least <chem> can present fractions simply, cleanly, and correctly without wacky workarounds:

 

Could something like this be implemented into chem2? I'd love to try my hand at fixing this issue rather than complain, but I'm afraid I have no idea where to even begin coding a template like this, much less edit one OlliverWithDoubleL (talk) 07:21, 25 October 2023 (UTC)Reply

I agree it would be good to get chem2 to support fractions nicely. It would be better than using 0.5 or doubling the numbers all around or using Unicode fractions. 1/2 covers most situations but there could be other factions coming up too. Graeme Bartlett (talk) 20:44, 25 October 2023 (UTC)Reply

Staggered Charges

edit

Isn't the IUPAC recommendation to stagger the charges? e.g. SO42-

See section 2.10.1 iv p51 of IUPAC (2007) Quantities, Units and Symbols in Physical Chemistry, Third Edition (The “Green Book”) https://iupac.org/wp-content/uploads/2019/05/IUPAC-GB3-2012-2ndPrinting-PDFsearchable.pdf

Ewen (talk) 13:11, 19 February 2024 (UTC)Reply

That ref is quite clear: "the staggered arrangement is now recommended", cited to:
  • N. G. Connelly, T. Damhus, R. M. Hartshorn, and A. T. Hutton, editors. Nomenclature of Inorganic Chemistry − IUPAC Recommendations 2005. The Royal Society of Chemistry, Cambridge, 2005.
and the current (2020) ACS Style Guide, §4.4.6 "Atoms & Molecules" (doi:10.1021/acsguide.40406) states "Stagger the subscript and superscript; do not align them. The subscript comes first with ionic charge." using "PO43-" as an example. DMacks (talk) 17:28, 19 February 2024 (UTC)Reply