Template talk:Chem2

Must preserve minus signs in subscripts
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)
 * I'm not familiar with this module, but the following lines of code might be relevant. Perhaps someone knows better? &mdash; Martin (MSGJ · talk) 17:35, 27 February 2023 (UTC)
 * 277:
 * 288:
 * 294:

debugging & tests

 * see /testcases at


 * More research:
 * Test string to be (rule: when final-{{hl|1=} }} in formula input, end pipe {{hl|1= }|}} }} is required):
 * (A)  &rarr; A_{x}H_{1-x}
 * Characters involved:
 * (H) &rarr; A_{x}H_{1-x}
 * (M) &rarr; A_{x}M_{1−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)
 * code review:
 * L239 (current live):
 * {{code|1=^\^{[^}]*}|2=}} ? expected {{code|1=^^{[^}]*}|2=}} (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)


 * test code to be:
 * Three minus/hyphen signs input, expected result: all three Minus (M). Current live test:
 * Live M>−<: A^{1−y}B_{1−x}M(2−)
 * returns H, H, M. sup/sub issue. -DePiep (talk) 20:43, 27 February 2023 (UTC)
 * returns H, H, M. sup/sub issue. -DePiep (talk) 20:43, 27 February 2023 (UTC)

Bug report

 * See /testcases at . module:Chem2, current live code.
 * (H)
 * (M)

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)
 * in H>-<: HA^{1-x}B2-B_{3-x}(4-) -- ❌ MINUS expected
 * in M>−<: MA^{1−x}B2−B_{3−x}(4−) -- ❌ MINUS expected
 * in H>-<: HA^{1-x}B2-B_{3-x}(4-) -- ❌ MINUS expected
 * in M>−<: MA^{1−x}B2−B_{3−x}(4−) -- ❌ MINUS expected


 * (A) Original complaint:
 * MINUS (−) not kept when in (topically correct) sub/supscripted formula is entered; shows hyphen (-) instead.
 * Cause: 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.
 * live, in:H>-<: HA^{1-x}B2-B_{3-x}(4-)
 * /sbx, in:H>-<:
 * live, in:M>−<: MA^{1−x}B2−B_{3−x}(4−)
 * /sbx, in:M>−<:
 * 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 (&amp;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.


 * Entering HTML Entity &minus;
 * live, in:>&amp;minus;<: EA^{1&minus;x}B2&minus;B_{3&minus;x}(4&minus;) -- incidentally, returns as expected
 * /sbx, in:>&amp;minus;<:
 * Entering &beta;
 * live, in:>&amp;beta;<: EA^{1&beta;x}B2&beta;B_{3&beta;x}(4&beta;) -- as expected :/sbx, in:>&amp;beta;<: -- same result (returns decoded &amp;beta; to UA: decoded later)


 * (C) Bad code
 * Parser gives error: {{code|1='^\^{[^}]*}'}} 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: . 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<: SsA^{1\sx}B2\sB_{3\sx}(4\s)
 * /sbx, in:>\s<:
 * live, in:>\-<: ShA^{1\-}B2\-B_{3\-x}(4\-)
 * /sbx, in:>\-<:
 * -DePiep (talk) 00:06, 28 February 2023 (UTC)

(T) more tests: bare hyphen: input without no preceding number etc in text element (eg, charge "(-)"):
 * HA^{-x}-B_{-x}(-)
 * MA^{−x}−B_{−x}(−)
 * EA^{&minus;x}&minus;B_{&minus;x}(&minus;) -- &amp;minus;
 * 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)
 * 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)
 * For more bare "-" input tests (not "2-" &tc;), see /testcases (UTC), -DePiep (talk) 06:36, 28 February 2023 (UTC)

Proposal (code edit)

 * From the Report above, I propose to oconsider: edit all code from module:Chem2/sandbox into module:Chem2.
 * Note: the (B) may change behaviour (instances relying on undocumented effects). Conservation action needed? OTOH, when entering &amp;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)
 * demos and tests stable & ok now, please take a look. -DePiep (talk) 06:41, 28 February 2023 (UTC)
 * Thanks, ! 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)
 * , any remarks? -DePiep (talk) 17:17, 2 March 2023 (UTC)
 * 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)
 * ✅ * Pppery * it has begun... 22:10, 4 March 2023 (UTC)

Proposal: add parameter checks
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, the final }-brackets are technically mixed up: closing bracket {..} of input and end of template brackets  }}.
 * result:  &rarr;  ❌
 * (B) is to check for such bad parameter effect. The page will be categorised.


 * Solutions (for the editor, /documentation):
 * add empty pipe: ..{..} (empty parameter #2);
 * add parameters numbers 1 and 2 (empty): ..{..};
 * add a space: ..{..}.


 * Both checks will give a warning.
 * Example, by /sandbox2:
 * &rarr;
 * This warning does not fix or alter the error.
 * Chem2 code used: Module:Chem2 (=current live code)
 * Tracking category is (new, one for all parameter checks).
 * Test code is in Chem2/sandbox2.
 * Testcases in.

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

demo
Use : All tests are with chem2/sandbox2 (/sandbox 2 ), and the live module.
 * Check 1, unknown parameter (preview)
 * A.  &rarr;
 * Check 2: threee end-brackets = ambiguous & wrong
 * B.  &rarr;  (err)
 * C.  &rarr;  (ok)
 * Check 3: extra pipe is used, but 2 has input
 * D.  &rarr;   -- (barfoo not correct)
 * E.  &rarr; {{chem2/sandbox2|CH4_{(g)}}  -- unbalanced brackets in input string
 * F.  &rarr; {{chem2/sandbox2|C{1}4_{(g)}}  -- unbalanced brackets in input
 * G.  &rarr; {{chem2/sandbox2|C1}4_(g)}}  -- unbalanced closing brackets in input


 * -DePiep (talk) 16:10, 6 March 2023 (UTC)

Template-protected edit request on 18 March 2023

 * Please replace all code from into
 * Notes: only template code, module not changed; and ../sandbox 2 is used (development reasons).
 * Changes: (1) parameters check by checkfup,
 * (2) extra check for end- } -bracket misreading with template-closing brackets }} (See )
 * Error category:.
 * Talk/tests/demo: see (preview warnings).
 * New code only warns/categorises. No changes to template output are made. DePiep (talk) 16:10, 18 March 2023 (UTC)
 * ✅ &mdash; Martin (MSGJ · talk) 14:12, 20 March 2023 (UTC)

Template-protected edit request on 20 March 2023
Please replace all code from Chem2 with Chem2/sandbox.

Change: bugfix. Adds auto, link to whitelist for check f.u.p. Was mistakingly missed in edit (by me). Causing to fill. DePiep (talk) 17:05, 20 March 2023 (UTC)
 * ✅.  P.I. Ellsworth &thinsp;, ed.  put'er there 11:18, 21 March 2023 (UTC)
 * Thanks. Tracking category is populated usefully now. DePiep (talk) 11:46, 21 March 2023 (UTC)
 * Yes, I saw that, my pleasure!  Paine  12:00, 21 March 2023 (UTC)

Roman numeral superscripts to indicate oxidation states
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)


 * Worth exploring indeed. (1) How would the 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}2O4 &rarr; Co^{II}Co^{III}2O4. Would this do, Ben? DePiep (talk) 11:45, 5 March 2023 (UTC)


 * 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)
 * 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)
 * The problem with  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   works fine. User:GKFXtalk 11:32, 13 March 2023 (UTC)
 * Right so. And "make it not too clever" is a fine reasoning. DePiep (talk) 10:08, 17 March 2023 (UTC)

The chem2 template works incorrectly on certain Wikipedia pages
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  and   normally gives CO3(2−) 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)
 * 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)
 * 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   element instead, but why not correct this annoying chem2 bug in the first place. Bernardirfan (talk) 18:37, 9 December 2023 (UTC)

Incorrect physical state notation
Putting physical state (like solid, gaseous, aqueous) to lower index does not seem to be correct, and examples like should be fixed to
 * CH4_{(g)}
 * CH4(g)

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

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


 * As the problem was with doc page, I've already it. —Mykhal (talk) 13:12, 2 October 2023 (UTC)
 * 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)

Fractions
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 1⁄undefined, $1⁄2$, or its cousin 1/undefined , $1/2$. Putting those templates into the chem2 template produces the following gnarly error:
 * 3 Me3SiCl + 3 Li + $1⁄2$ N2 -> (Me3Si)3N + 3 LiCl

Sure, one could split the template up and include 1⁄undefined midway, but then you'd have to encase the whole thing in 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?

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)
 * 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. covers most situations but there could be other factions coming up too. Graeme Bartlett (talk) 20:44, 25 October 2023 (UTC)

Staggered Charges
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)
 * 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" (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)