Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Debilski's post is definitely a solid way to do it, although the <code>\say</code> is not necessary. Here's a shorter way that makes use of some LaTeX internal shortcuts (<code>\@gobble</code> and <code>\@ifnextchar</code>):</p> <pre>\makeatletter \def\hyphenatestring#1{\xHyphen@te#1$\unskip} \def\xHyphen@te{\@ifnextchar${\@gobble}{\sw@p{\hskip 0pt plus 1pt\xHyphen@te}}} \def\sw@p#1#2{#2#1} \makeatother</pre> <p>Note the use of <code>\hskip 0pt plus 1pt</code> instead of <code>\discretionary</code> - when I tried your example I ended up with a ragged margin because there's no stretchability. The <code>\hskip</code> adds some stretchable glue in between each character (and the <code>\unskip</code> afterwards cancels the extra one we added). Also note the LaTeX style convention that "end user" macros are all lowercase, while internal macros have an <code>@</code> in them somewhere so that users don't accidentally call them.</p> <p>If you want to figure out how this works, <code>\@gobble</code> just eats whatever's in front of it (in this case the <code>$</code>, since that branch is only run when a <code>$</code> is the next char). The main point is that <code>\sw@p</code> is only given one argument in the "else" branch, so it swaps that argument with the next char (that isn't a <code>$</code>). We could just as well have written <code>\def\hyphenate#next#1{#1\hskip...\xHyphen@te}</code> and put that with no args in the "else" branch, but (in my opinion) <code>\sw@p</code> is more general (and I'm surprised it's not in standard LaTeX already).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload