3
$\begingroup$

Whenever I want to write a solution to a question I write it in LyX. Then I go the the code (I can see it in the bottom of the page) and I copy-paste it into the site.

There are small problems with this approach - LyX generates "\[" and "\ ]" for inline instead of the

$$

sign. Another example is that LyX generates \textbf{BoldedText} instead of

**BoldedText**

So I have to manually change those. I assume that many here are using LyX - did any of you find a workaround so I can just copy-paste the code generated by LyX ?

$\endgroup$
  • $\begingroup$ If you are familiar with sed, then I suppose you could write a sed-script for this. (I am mentioning sed simply because that is the tool I am - to some extent - familiar with. There are certainly many other tools; this could be probably done with awk, perl or some other tool.) $\endgroup$ – Martin Sleziak Oct 15 '14 at 14:32
  • 1
    $\begingroup$ For somewhat the "converse direction" see meta.math.stackexchange.com/questions/15230/… $\endgroup$ – quid Oct 15 '14 at 14:36
  • 3
    $\begingroup$ @Martin: I don't know how many people who are well-versed in any of the tools you suggest would prefer LyX over LaTeX. :-) $\endgroup$ – Asaf Karagila Oct 15 '14 at 17:30
  • 1
    $\begingroup$ @AsafKaragila Actually, nothing in the question is LyX-specific, it's just the normal LaTeX markup that does not work on this site. As quid correctly noted, the conversion is the inverse map of "Save as LaTeX" script that I put together earlier. (That one is much more comprehensive). $\endgroup$ – user147263 Oct 15 '14 at 19:04
6
$\begingroup$

You can use the bookmarklet Convert LyX-produced markup (drag it to the bookmark bar). First, copy-paste your code into the answer or question box; then click the bookmarklet. It makes the following replacements on the text in the box:

  1. \textbf{text} to **text**
  2. \textit{text} to *text*
  3. \[formula\] to $$formula$$

Note that the preview window does not refresh automatically when the replacements are made; to make it refresh, type some character (e.g., a space) in the editor window.

Since I don't use LyX myself, I don't know what other replacements are needed. Feel free to add them in a comment. Or add them to the script yourself: the source code is at the bottom of my bookmarklets page (and it's not long).


Explanation of the script:

  • The line a=a.replace(/\\\[/g,'$$$$'); says: Replace every occurrence of \[ by $$. (Since \ and [ have special meaning for sed, they have to be escaped.)
  • The following line is the same for \]
  • The line a=a.replace(/\\textbf{([^}]*)}/g,'**$1**'); is a bit more complicated. It searches for \textbf{. Then the script finds the part consisting of all characters different from }. (The string [^}] says any character different from }. The * asterisks denotes repeating, so [^}]* matches any string consisting of characters different from ]. Then the script replaces \textbf{ with ** and } with **. The part between them is left unchanged. (The $1 means that at this place should the part of the matching string enclosed between ( and ).)
$\endgroup$
  • $\begingroup$ Care Bear: I was in the process of writing an answer with a sed script. (Which is now not needed.) My unfinished answer contained also some explanation of the script. As syntax you're using seems similar to to sed, it could simply copy it to your answer with only minor changes. (I thought it might be useful for the OP. And I did not want my work to be completely in vain.) Feel free to remove it from your answer if you don't like it. $\endgroup$ – Martin Sleziak Oct 15 '14 at 18:21
  • $\begingroup$ BTW my sed script did not work for example in cases where \textbf ended on the following line: \textbf{but this does not<br> work}. (By <br> I mean newline, which I am unable to simulate in a comment.) And also in cases when the parameter of \textbf contains {}. \textbf{and neither does $x^{2n}$ this}. I wonder whether things like these might be problem for your bookmarklet, too. $\endgroup$ – Martin Sleziak Oct 15 '14 at 18:24
  • $\begingroup$ It's the regular expression syntax, which is used in many languages (in my case, JavaScript). The linebreaks are not a problem for the bookmarklet. Since \textbf{bold text with $x^{2n}$ in it} breaks the regex, I changed it to /\\textbf{([^}\$]*)}/; so that no replacement is made if the bold text contains formulas. ("do no harm") People should not be writing that much bold or italic text to begin with, and if they do, they'll have to convert manually. $\endgroup$ – user147263 Oct 15 '14 at 18:59
  • $\begingroup$ Thanks! Works great, just what I needed $\endgroup$ – Belgi Oct 16 '14 at 9:00
  • $\begingroup$ Martin thank you for your efforts as well! $\endgroup$ – Belgi Oct 16 '14 at 9:00

You must log in to answer this question.

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