7
$\begingroup$

I'm definitely not the first to think about having the possibility users having personalised $LaTeX$ \newcommands (see e.g. here). But I thought of another way to do this while avoiding (I think) the problems mentioned in the linked thread. What if we could define \newcommands somewhere in our account, write the answer/question and before submitting click a little "magic button" that replace all calls to these \newcommands by their associated $LaTeX$ codes? It would be a sort of "multi-search/replace" function running through the post.

To be more clear here's an example:

  • I define somewhere in my account \newcommand{\trace}{\operatorname{Tr}}

  • I write an answer like:

    Note that \$\trace(A)\neq 0\$ and thus \$A\neq 0\$. blabla...

  • I press the "magic button" and my answer gets automatically transformed into

    Note that \$\operatorname{Tr}(A)\neq 0\$ and thus \$A\neq 0\$. blabla...

  • I submit my answer

This would avoid the three problems listed by Grigory M in the linked thread (if I understood them well). In worst case, if you define a lot of commands then it gets slow for you when you press the "magic button" but this doesn't affect the other members of the community.

I don't know how easy/hard this would be to implement but I'm interested to have your opinion on this feature and what kinds of problems it might raise.

$\endgroup$
  • 3
    $\begingroup$ One possible drawback is that your notation may sometimes slightly mismatch the question (the question could have "tr" instead of "Tr", for example). I usually try to answer using the notation identical to the question. But for common operators like Re and Im this would be useful... but more likely to be implemented by some user as a userscript, than by SE. $\endgroup$ – user147263 Jan 7 '15 at 18:30
  • $\begingroup$ interesting (and indeed it avoids that 3 problems I think). btw, this can be implemented by a browser extension — perhaps something like this exists already?.. $\endgroup$ – Grigory M Jan 7 '15 at 18:34
  • $\begingroup$ @Fundamental this is a good point, however changing the Tr in tr after pressing the button is probably quicker than writing \operatorname{tr} each time (but this raise the chance of have different notations in the same answer...). $\endgroup$ – Surb Jan 7 '15 at 18:35
  • $\begingroup$ @GrigoryM If you cross something like that on your way, I'd be interested to test it :) $\endgroup$ – Surb Jan 7 '15 at 18:35
  • $\begingroup$ Google tells that Popchrom extension seems to be capable of something like this $\endgroup$ – Grigory M Jan 7 '15 at 18:39
  • $\begingroup$ You could consider writing your own AutoHotKey script. They can really help in writing TeX much faster, and more accurate as well. $\endgroup$ – Lord_Farin Jan 7 '15 at 18:45
  • 1
    $\begingroup$ Wouldn't that feature break (or half-break) preview? You'd only get a proper review after pressing the button, if I understand correctly. Not a deal-breaker, but something to consider, I think. $\endgroup$ – Daniel Fischer Jan 7 '15 at 18:52
  • $\begingroup$ @DanielFischer this is another very goodpoint however we could immagin to be possible to press the button in the middle of writing the answer when the user wants to see the preview. $\endgroup$ – Surb Jan 7 '15 at 19:49
  • $\begingroup$ Seems like this is just an aliasing thing, surely there must be some existing Javascript (or text editor) plugin that would get the job done. $\endgroup$ – Alexander Gruber Jan 8 '15 at 2:18
6
$\begingroup$

Specifically for operator names, the bookmarklet operator names does the job. Its syntax is \name%; I picked % as delimiter to minimize side effects, but if someone has a better idea, let me know. For example, typing \tr% results in \operatorname{tr}.

You can execute the bookmarklet at any point of writing a post; it will re-run every second until you leave the page or reload it. This addresses the point brought up by Daniel Fischer: the substitution should be timely so that the preview works.

The code is quite simple: I include it below. Other substitutions can be implemented similarly, but I don't know what they should be. (One thing I'm considering for myself is \bb => \mathbb and similarly for other font commands.)

var editor = document.getElementById('wmd-input');
if (editor) {convertInput()}

function convertInput() {
  editor.value = editor.value.replace(/\\(\w+)%/g, '\\operatorname{$1}');
  window.setTimeout(convertInput,1000);
}
$\endgroup$
  • $\begingroup$ This is very nice! However, it seems to not work when editing a question or an answer (only during the first shot) and it transforms also the expressions when they are not between \$...\$ (although I don't really see why it is a problem because I don't remember writing \abc% in texts). $\endgroup$ – Surb Jan 8 '15 at 8:59
  • $\begingroup$ Thanks for the bug report. I updated the bookmarklet: the new version should work for edits too. (Changed the first line to var editor = document.querySelector('.wmd-input.processed');). False positives outside of math mode seem unlikely; in any case there is no easy and reliable way to detect what's in math mode. $\endgroup$ – user147263 Jan 8 '15 at 20:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .