Using LyX to write answers for posts

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 ? • 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.) – Martin Sleziak Oct 15 '14 at 14:32 • For somewhat the "converse direction" see meta.math.stackexchange.com/questions/15230/… – quid Oct 15 '14 at 14:36 • @Martin: I don't know how many people who are well-versed in any of the tools you suggest would prefer LyX over LaTeX. :-) – Asaf Karagila Oct 15 '14 at 17:30 • @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). – user147263 Oct 15 '14 at 19:04 1 Answer 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 ).)
• 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. – Martin Sleziak Oct 15 '14 at 18:21
• 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. – Martin Sleziak Oct 15 '14 at 18:24
• 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. – user147263 Oct 15 '14 at 18:59
• Thanks! Works great, just what I needed – Belgi Oct 16 '14 at 9:00
• Martin thank you for your efforts as well! – Belgi Oct 16 '14 at 9:00