Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax definitions for Chemformula package. #2235

Closed
Neelfrost opened this issue Nov 8, 2021 · 8 comments
Closed

Syntax definitions for Chemformula package. #2235

Neelfrost opened this issue Nov 8, 2021 · 8 comments

Comments

@Neelfrost
Copy link

Chemformula is a comprehensive package for chemistry-related typesetting. It features a single main command: \ch{}.

VimTeX offers a very handy function to check whether the cursor (scope?) is currently in a "mathzone". This is really useful in defining math-specific snippets. I would like to replicate this functionality with \ch{}.

The \ch{} command has 2 use cases:

  1. In line with text: Zinc deposition (\ch{Zn^{2+} + 2 e^{-} -> Zn}) at \SI{<-0.76}{\V}.
  2. Inside a math environment:
\begin{gather(align, etc.)}
  \beforetext{Metal oxidation} \ch{M -> M^{n+} + ne^{-}}
\end{gather}

Describe the solution you'd like
Since vimtex#syntax#in_mathzone() relies on syntax definitions, I would like similar syntax definitions for \ch{}.

Describe alternatives you've considered
I have tried the following:

syntax match texChemCmd nextgroup=texChemArgs skipwhite "\\ch\>"
call vimtex#syntax#core#new_arg('texChemArgs', {'opts': 'contained transparent'})

This works for inline \ch, but not within math environments.

@lervag
Copy link
Owner

lervag commented Nov 8, 2021

Seems useful to implement. From the docs it seems the following examples should give a relatively complete sense of what needs to be supported here.

\ch[〈options〉]{〈input〉}
\ch[decimal-marker={,}]{3.5}
\ch[frac-style=xfrac]{3/2}
\ch{A + B ->[a] C}

\ch{ "escaped text" }
\ch{ 'escaped text' }
\ch{"\ldots\," Na + "\ldots\," Cl2 -> "\ldots\," NaCl}

\ch{ $escaped math$ }
\ch{ \(escaped math\) }
\ch{$2n$ Na + $n$ Cl2 -> $2n$ NaCl}

I'll work on it when I get the time!

@Neelfrost
Copy link
Author

Looking forward to it. Thank you!

lervag added a commit that referenced this issue Nov 9, 2021
@lervag
Copy link
Owner

lervag commented Nov 9, 2021

Can you update and test now? It may not be perfect, but I believe it should be an improvement already.

@Neelfrost
Copy link
Author

Neelfrost commented Nov 9, 2021

It works perfectly, however, it does not work when chemformula is imported by chemmacros.

MWE:

\documentclass{article}
\usepackage{chemmacros}
\chemsetup{%
  formula = chemformula,
}

\begin{document}

\ch{M -> M^{n+} + ne^{-}}
\ch{A + B ->[a] C}

\ch[options]{input}
\ch[decimal-marker={,}]{3.5}
\ch[frac-style=xfrac]{3/2}

\ch{ "escaped text" }
\ch{ 'escaped text' }
\ch{"\ldots\," Na + "\ldots\," Cl2 -> "\ldots\," NaCl}

\ch{ $escaped math$ }
\ch{ \(escaped math\) }
\ch{$2n$ Na + $n$ Cl2 -> $2n$ NaCl}

\begin{equation}
  \ch{M -> M^{n+} + ne^{-}}
\end{equation}

\end{document}

@lervag
Copy link
Owner

lervag commented Nov 9, 2021

It works perfectly, however, it does not work when chemformula is imported by chemmacros.

Ah, yes; if you load chemformula implicitly, then it won't load until you compile the document. This is because VimTeX only loads the syntax for packages that it detects, and detection requires either the \usepackage lines in the preamble or the .fls file (after compilation).

@Neelfrost
Copy link
Author

Okay, loading chemformula implicitly along with chemmacros does not seem to produce any errors, and syntax also works, so I'll take this as a win. Thank you for the quick update! Much appreciated :)

@lervag
Copy link
Owner

lervag commented Nov 9, 2021

Okay, loading chemformula implicitly along with chemmacros does not seem to produce any errors, and syntax also works, so I'll take this as a win.

Great. Note, I could of course load chemformula explicitly from chemmacros, but going that path would imply a huge effort if I want to support all kinds of latex package dependencies. So, instead, I think living with the minor inconvenience that we have to parse the .fls file to load the implicit packages is a pragmatic and good solution.

Thank you for the quick update! Much appreciated :)

My pleasure; thanks for raising the issue!

@Neelfrost
Copy link
Author

Great. Note, I could of course load chemformula explicitly from chemmacros, but going that path would imply a huge effort if I want to support all kinds of latex package dependencies.

Yeah, I understand, doing that would be overkill IMO because not many packages import other packages.

lervag added a commit that referenced this issue Dec 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants