# MathJax re flex

MathJax mirror on the wall, what is the fairest code of all?

Let’s find the minimal replacement for (replace this line) in the following MathJax code ...

$$\require{begingroup}\begingroup (replace this line) |Medusa| \endgroup$$


... so that the rendered result is the reverse of $\small |Medusa|$ :

$\kern5em \small\texttt{ } |asudeM|$

The catch?    Any subsequent letters-only edits to the original Medusa should automatically be reflected in the result. Thus, the following ...

$\kern5em \small\texttt { \$\$\require{begingroup}\begingroup }$
$\kern5em \small\texttt { (replacement lines) }$
$\kern5em \small\texttt { |MeDuSaEDITED| } \Large\raise-1mu\strut$
$\kern5em \small\texttt{ \endgroup\$\$}$

... would render as:

$\kern5em \small\texttt{ } |DETIDEaSuDeM|$

Letters-only edits include the addition or deletion of any letters a through z, including uppercase, between the mirror’s | | edges. All letters might even be removed.

Spaces and line breaks are encouraged for readability, so they do not count in measuring the replacement code. Do not use an additional $\small\texttt{\require{...}}$ or defeat the final call to $\small\texttt{\endgroup}$. Admittedly, the audience for this puzzle is small. Please share any answer that even almost works.

\def\G#1
{\~#1 \~ %}
\def\~#1#2 #3{#3
#2 #3#1}
\G
|Medusa|
\endgroup$$ Finally, this is the self-tracing code:$$\require{begingroup}\begingroup \def\>#1#2 #3#4#5.{\texttt{#1 #2}&\longrightarrow&\texttt{#3}\\&&\texttt{#5}\\[5px]#3#4#5.} \def\G#1 {\>\G[#1] \~#1 \~ %. } \def\~#1#2 #3{\>\~[#1][#2][#3] #3 #2 #3#1} \begin{array}{lcl} \G |Medusa| \end{array} \endgroup$$In the last line of the trace, the first % is followed by a line break, while the last one is not, so the first comments out nothing, while the second comments out the following \~. ## Previous Solution Based on all the excellent work of @humn, I have a modified version of his 2-\def solution. It incorporates two ideas to reduce the number of characters: first, the definition of \G can be modified to use the line break as a delimiter rather than giving the | characters explicitly; this saves 3 characters. Second, we can use spaces as the delimiters for the buckets rather than |, saving 4 characters. The use of spaces for delimiters changes how the termination works (as explained below), and it turns out that {\let\ } is no longer needed (7 characters), but three empty parameters {} are needed (6 characters), for a 1-character savings. So the total savings are 8 characters, which brings this solution in at 46 characters and 2 \def statements.$$\require{begingroup}\begingroup \def\G#1 {\~#1 \~ {}{} {}} \def\~#1#2 #3{#3#2 #3#1} \G |Medusa| \endgroup$$‍\require{begingroup}\begingroup \def\G#1 {\~#1 \~ {}{} {}} \def\~#1#2 #3{#3#2 #3#1} \G |Medusa| \endgroup‍  Note that the line break in the definition of \G is required (it is the delimiter for the #1, and so #1 will be |Medusa|, including the vertical lines). Here is the live trace of the code in operation, with "output" from the program shown between the lines indicating the macro substitutions (the |asudeM| shows up between the last two substitutions).$$\require{begingroup}\begingroup \def\>#1#2 #3\ {\\\texttt{#1 #2}&\longrightarrow&\texttt{#3}\\#3\ } \def\G#1 {\>\G[#1] \~#1 \~ {}{} {}\ } \def\~#1#2 #3{\>\~[#1][#2][#3] #3#2 #3#1} \begin{array}{lcl} \G |Medusa| \end{array} \endgroup$$Here's how it works: as with @humn's solution, the reversed version of the text is stored after the \~ that is recycled for the recursive calls, but before the {} that is used to terminate the loop. How does that termination occur, here? The code takes advantage of the way that spaces are treated during parameter collection. For undelimited parameters, like the #1 in \~ that is followed immediately by #2, the next non-space character (or braced group of characters) is taken as the value of the parameter. Since we are using spaces as delimiters, when the original text is exhausted, the space delimiter will be the first thing following the \~, and it will be skipped when determining the #1 parameter, so #1 will become \~ (note that in the @humn solution, the | delimiter becomes #1 at this point.) That means that #2 is the reversed text (including the delimiters) and #3 is empty (due to the first {}), rather than the \let\ of @humn's solution. This is shown in the line of the trace just before the |asudeM| shows up. When the replacement text is formed, it starts with #3, which is empty (rather than the usual \~); that stops the loop. The #2 that follows inserts the reversed text, which is output at that point. This is followed by a space (which produces no output) and then by #3 (which is empty), and finally by #1, which is the \~. But because \G included {} {} at the end of its substitution string, this last copy of \~ ends up with all its parameters being empty, so it outputs nothing, and the execution is complete. So a big difference here is that stopping the loop is accomplished by using an empty command. This differs from the @humn solution that uses \let cleverly to remove the looping macro (while at the same time cleaning up the trailing loop macro). The last thing to note is that \~ is used here rather than \R. The reason for this is that when MathJax makes a parameter substitution that ends with a macro whose name consists of alphabetic characters, and the first character following the substitution is also alphabetic, it inserts a space so that the macro name is not misinterpreted (the following alphabetic characters would have been added to the name). This can be seen in the live trace for @humn's top solution, where the reversed text has spaces between each letter. Because we are using spaces as delimiters, we can't afford to have these spaces inserted by MathJax, so we change to a macro that has a non-alphabetic name; MathJax doesn't insert spaces for that case. I hope that makes the operation of this solution clear. ## Initial Solution This is my original solution. It is 75 characters, if I counted correctly.$$\require{begingroup}\begingroup \def\G#1|#2|{\R :#2|\R|\S} \def\R#1:#2#3|#4{#4 #2#1:#3|#4} \def\S#1:#2\S{#1|} \G |Medusa| \endgroup$$‍\require{begingroup}\begingroup \def\G#1|#2|{\R :#2|\R|\S} \def\R#1:#2#3|#4{#4 #2#1:#3|#4} \def\S#1:#2\S{#1|} \G |Medusa| \endgroup‍ Here is the run history, with the parameters for the leading macro listed at the right. \G |Medusa| #1= #2=Medusa \R :Medusa|\R|\S #1= #2=M #3=edusa #4=\R \R M:edusa|\R|\S #1=M #2=e #3=dusa #4=\R \R eM:dusa|\R|\S #1=eM #2=d #3=usa #4=\R \R deM:usa|\R|\S #1=deM #2=u #3=sa #4=\R \R udeM:sa|\R|\S #1=udeM #2=s #3=a #4=\R \R sudeM:a|\R|\S #1=sudeM #2=a #3= #4=\R \R asudeM:|\R|\S #1=asudeM #2=| #3=\R #4=\S \S |asudeM:\R|\S #1=|asudeM #2=\R|\S |asudeM|  I use \G to set up the recursion (just as Humn does). The \R macro is the heart of the matter, doing the recursive reversing, and \S is the "stop" macro, doing the cleanup and ending the process. The original and reversed text are stored in two "buckets": the reversed text first, followed by a colon, followed by the original text, followed by a vertical line (|). The \R macro uses #1:#2 to read the current reversed text and the first character of the original text and moves the character to the beginning of the reversed text via #2#1: while inserting \R at the beginning again to continue the recursion. The key is how to stop the recursion. This is done in \R by reading the next command to perform (as well as the text buckets). The #3| reads the rest of the unreversed text, and then #4 reads the command to perform. The replacement text #4 #2#1:#3|#4 puts the command first, then the reversed text so far, the colon, the rest of the unreversed text, the delimiter, and the next command (so the recursion continues). To see why this works, you need to consider the situation when the unreversed text is empty. In this case, we have \R asudeM:|\R|\S.  and so #1 is asudeM, and #2 is |, the next character, since that is the next character in the input. Then #3 is everything up to the next |, which is the \R, and #4 is the next token, \S. That is, we take advantage of the fact that #2 eats the unreversed-text delimiter when the unreversed text is empty, and so #3 eats the \R, and we end up reading \S as #4 rather than the usual \R. That stops the recursion. Then \S is left to clean up the buckets and output the final text. Note that it would be possible for \S to isolate the reversed text and do what ever it wants with that, via \def\S|#1:#2.{...} so although we take advantage of the fact that the trailing delimiter is the same as the initial one, that need not be the case. That is, we could reverse [Medusa] as [asudeM] rather than ]asudeM[ if desired. • I suppose one could have \G redefine itself to be essentially \S and use \G in place of \S. But while that is two names, it is still really three macros. I haven't considered the other problem, but your macros do seem pretty concise for that. Jul 16 '16 at 13:03 • MathJax does not include a tracing facility like this (though it is an interesting idea). I made my listing by hand. The error messages are turned off by default on SE, and so when I redid the editing code for them a few months ago, I made sure to re-endable them during editing. I'm glad you appreciate the change. Jul 16 '16 at 21:41 • 46 characters = 8 fewer than what had seemed irreducible without cheating! This answer not only demonstrates ever more versatile use of delimiters but also once again improves the puzzle as a whole by sharing leaps in a solution process. – humn Jul 28 '16 at 19:07 • Click for an image of a comment with line breaks. It shows a slightly smaller version hiding inside your present solution. Not different enough for me to post legitimately, but similar enough that you could personalize it. – humn Jul 30 '16 at 12:41 • Was about to \color{#0c0}{\large\raise-1mu\checkmark} this but thought of a way to get one \small\texttt{\def} closer to a no-\small\texttt{\def} solution. Luck got it to 34 chars, of which 3 still feel like surplus. Any lower and I won't be able to resist posting. Meanwhile, here's a likely-final preview of the dragon-like puzzle. Probably take longer for me to finish its presentation than for you to solve it. (Then back to \small\texttt{\binary{}} and \small\texttt{\stars{}}, with ever more perspective and ever fewer tokens.) – humn Aug 8 '16 at 22:52 Last solution$$\require{begingroup}\begingroup \def \G |#1|{ | \R #1 |\R |{\let\ } } \def \R #1#2|#3{ #3 #2 |#3 #1 } \G |Medusa| \endgroup$$This has 54 non-space replacement characters and exploits two economies: • \small\texttt{\\}\color{violet}{\LARGE\raise-3mu\square} control space (backslash and space) is a control sequence but counts as only one character • By lurking behind the mirror until cleanup time, ~\small\texttt{\{\let\\}\color{violet}{\LARGE\raise-3mu\square}\texttt{\}}~ acts like a macro without using \small\texttt{\def} \require{begingroup}\begingroup \def \G |#1|{ | \R #1 |\R |{\let\ } } \def \R #1#2|#3{ #3 #2 |#3 #1 } \G |Medusa| \endgroup  The live-tracing version below can be fun to play with in a temporary Ask Question window, where you can edit the \small\texttt{|Medusa|} portion. Randomly adding letters or spaces elsewhere can also be amusing. \small\texttt{\\}\color{violet}{\LARGE\raise-3mu\square} \, is represented by~\, \small\texttt{\_} \,. One way to extract the self-tracing code (be sure to add \small\texttt{\\} around it) is by right-clicking on the trace and selecting  \small \bbox[6px,border:1px solid black]{ \textsf {Show Math As} ~ \raise1mu\blacktriangleright }\kern-4mu\raise-5mu \bbox[6px,white,border:1px solid black]{ \textsf{TeX Commands} }$$\require{begingroup}\begingroup \def \d #1{ \kern1mu \raise-8mu { \scriptsize\texttt {#1} } \kern1mu } \def \t #1{ { \texttt {#1} } } \def \s #1{ { \small\texttt{#1} } } % \let \LET=\let \def \let #1#2#3\end{ \LET \let=\LET & \to~~~ | \LET#1#2#3\end } \def \G |#1|{ \t{\G|#1|} \kern 16mu & \to~~~ | ~ \rlap{ \s{\R #1|\R|{\let\_}} } \\ \R #1|\R|{\let\_} } \def \R #1#2|#3#4\end{ \t{\R}\d'\s{#1}\d'\s{#2}\d{|}\s{#3} & \to~~~ | ~ \rlap { \s{#3 #2|#3#1} } & \kern -1em \s{#4} \\ \kern-1em #3 #2|#3#1#4\end } \begin{array}{rlr} \G |Medusa| \end{array} \endgroup$$. .-------------------------------. . .---- | ----------------------. | . | | | | . V |.--. | . #1 #2 | #3 | | . | \G Medusa '--' V . | \R M edusa | \R | {\let\_} (reversal . | \R e dusa | \R M | {\let\_} steps) . | \R d usa | \R eM | {\let\_} . | \R u sa | \R deM | {\let\_} . | \R s a | \R udeM | {\let\_} . | \R a - | \R sudeM | {\let\_} . . . . . . . . . . . .' . . . . . . .' . . . ' ' (delimiter . . . '.------------------------ ' --------. shift) . .- | ----------------------. ' | . | | | ' | . | | ' .---. | . | \R | | \R asudeM | {\let\_} | | (cleanup) . V | V . | . | \let\_ \R asudeM | \let\_ | | . '-----' . | asudeM | .  Davide Cervone’s answer well explains how “buckets” can be used as variables and how macro calls can be stored in upcoming text to control recursion. This solution has 5 conceivably non-essential characters, as the approach cannot avoid using the following 49 characters in some combination. \def \G |#1|{ | \R #1 | \R \let } \def \R #1#2|#3{ #1 #2 #3 #3 } \G  Non-space replacement characters can be whittled down to 50 by, what else, cheating, Invisibly at that, and in two ways. Can you tell how?$$\require{begingroup}\begingroup \def \G #1 { \R #1@\ \R \ \let@ } \def \R #1#2\ #3{ #3 #2\ #3 #1 } \G |Medusa|   \endgroup$$$$\require{begingroup}\begingroup
\def \G   #1 {        \R   #1@\   \R   \ \let@   }
\def \R  #1#2\ #3{   #3    #2\   #3   #1        }
\G
|Medusa|
\endgroup$$ One cheat is in the code above — remember, it is invisible — and the other is revealed by rendering an illegitimate trailing \, \small\texttt{\\}\color{violet}{\LARGE\raise-3mu\square \,} control space, again represented by \, \small\texttt{\_} ~ in the diagram.$$\require{begingroup}\begingroup \def \_ {\color{violet}{\LARGE\raise-3mu\square}} \def \G #1 { \R #1@\_ \R \_\let@ } \def \R #1#2\_#3{ #3 #2\_ #3 #1 } \G |Medusa|   \endgroup$$. .----------------------------------. . .---- | ----------------------. | . | | | | . V |.--. | . #1 #2 \_ #3 | | . \G |Medusa| '--' V . \R | Medusa|@ \_ \R \_ \let@ . \R M edusa|@ \_ \R | \_ \let@ . \R e dusa|@ \_ \R M| \_ \let@ . \R d usa|@ \_ \R eM| \_ \let@ . \R u sa|@ \_ \R deM| \_ \let@ . \R s a|@ \_ \R udeM| \_ \let@ . \R a |@ \_ \R sudeM| \_ \let@ . \R | @ \_ \R asudeM| \_ \let@ . \R @ - \_ \R |asudeM| \_ \let@ . . . . . . . . . . .' . . . . . . . . .' . . ' ' . . '.----------------------- ' --------. . .- | ----------------------. ' | . | | |' | . | |' .--. | . \R | \_ \R@|asudeM| \_ \let | | @ . V | V . | . \let \R@|asudeM| \_ \let | \_ @ . '--' . |asudeM| \_ .  ### Early non-solution to this puzzle, though a solution to a possibly better puzzle Here’s an intermediate result previously offered as an example of how some things might work, juxtaposing the original \small|Medusa| with its reverse, rendered first as a trace and then just the result. $$\require{begingroup}\begingroup
\def \go         |#1|{   \rep   |           #1 \stop   |        }
\def \rep      #1#2|{         #1   \rep    #2         |   #1   }
\def \stop  #1\stop{                                 |        }
\go
|Medusa|
\endgroup$$$$\require{begingroup}\begingroup \def \d #1{ \kern1mu \raise-8mu { \scriptsize\texttt {#1} } \kern1mu } \def \t #1{ { \texttt {#1} } } \def \s #1{ { \small\texttt{#1} } } % \def \go |#1|{ \t {\go|#1|} \kern 31mu ~~\to \kern-11mu & & \s{\rep |#1\stop|} \\ \rep |#1\stop| } \def \rep #1#2|#3\end{ \t {\rep}\d'\s{#1}\d'\s{#2} ~~\to \kern-11mu & \s{#1} \kern-9mu & \s{\rep#2|#1#3} \\ #1~~~ \rep#2|#1#3\end } \def \stop #1\stop#2\end{ \t{\stop}\d'\s{\rep|}\d{\stop} ~~\to \kern-11mu & & | #2 \end } \begin{array}{rrl} \go |Medusa| \end{array} \endgroup\large =\require{begingroup}\begingroup \def \go |#1|{ \rep | #1 \stop | } \def \rep #1#2|{ #1 \rep #2 | #1 } \def \stop #1\stop{ | } \go |Medusa| \endgroup$$This is the result originally intended for this puzzle, because it displays the source and its reverse together, but got simplified ☆ at post time as I didn’t yet know what a solution might really involve. The code for this turned out to be simpler, though, than the code for the simpler-looking result, while still being tricky enough to be puzzle-worthy. Mechanism:  \small\texttt{\rep}  leapfrogs letters by replicating until it lands after  \small\texttt{\stop} . Then  \small\texttt{\stop}  eats  \small\texttt{\rep} . \go | M e d u s a | \rep | M e d u s a \stop | . | \rep M e d u s a \stop | | . | M \rep e d u s a \stop | M| . | M e \rep d u s a \stop | eM| . | M e d \rep u s a \stop | deM| . | M e d u \rep s a \stop | udeM| . | M e d u s \rep a \stop | sudeM| . | M e d u s a \rep \stop | asudeM| . | M e d u s a \stop \rep | \stop asudeM| . | M e d u s a | asudeM|  Borrowing attributes from later solutions boils this down to 52 non-space replacement characters.$$\require{begingroup}\begingroup \def \G |#1|{ \R | #1{\def\R{}} | } \def \R #1#2|{ #1 \R #2 | #1 } \G |Medusa| \endgroup$$$$\require{begingroup}\begingroup
\def \G    |#1|{   \R   |         #1{\def\R{}}   |        }
\def \R  #1#2|{       #1   \R    #2             |   #1   }
\G
|Medusa|
\endgroup$$ ### Initial solution This has had 92 non-space replacement characters before Davide Cervone’s refinement improved it to 87 ( 84 79 if macro names were further abbreviated). This failed to recognize that  \small\texttt{\@}  could assume control at the right time without help from \,~\small\texttt{\[...]} . Correcting that did, however, lead to the 54-character solution above.$$\require{begingroup}\begingroup \def \go |#1|{ \rep | #1 \@ | } \def \rep #1#2|{ \[ #1 \@ \rep #2 | ] #1 } \def \[ #1\@#2]{ #2 } \def \@ #1\@{ | } \go |Medusa| \endgroup$$$$\require{begingroup}\begingroup
\def  \go       |#1|{   \rep   |                #1 \@   |            }
\def  \rep    #1#2|{    \[   #1   \@   \rep    #2      |   ]   #1   }
\def  \[   #1\@#2]{                           #2                   }
\def  \@     #1\@{                                   |            }
\go
|Medusa|
\endgroup$$$$\require{begingroup}\begingroup \def \d #1{ \kern1mu \raise-8mu { \scriptsize\texttt {#1} } \kern1mu } \def \t #1{ { \texttt {#1} } } \def \s #1{ { \small\texttt{#1} } } \def \go |#1|{ \t{\go|#1|}\kern7mu \\ \rep |#1\@| } \def \rep #1#2|#3\end{ \t{\rep}\d'\s{#1}\d'\s{#2}\d| & \kern -9mu \s{#3} & \kern-1em \[ #1\@\rep#2|]#1#3\end } \def \[ #1\@#2]#3\end{ \t{\[}\d'\s{#1}\d{\@}\s{#2}\d] & \kern-9mu \s{#3} \\ #2#3\end } \def \@ #1\@{ \t{\@}\d'\s{#1}\d@ &\raise4mu\strut & | } \begin{array}{rlrl} \go |Medusa| \end{array} \endgroup

The mechanism borrows from the earlier non-solution above, but lands an additional role for $\small\texttt {\@} \,$, previously named $\small\texttt {\stop} \,$, giving it triple duty in macros: (1) Interior delimiter of the $\small\texttt{ \[...\@...] }$ vanishing-act macro; (2) name and (3) terminal delimiter of the $\small\texttt{ \@...\@ }$ macro that halts the $\small\texttt{ \rep...| }$ loop by $\small\texttt{ \@\rep...|\@ }$ swallowing it.

\go   |          M          e          d            |

\rep |          M          e          d           \@           |
\[  | \@  \rep M          e          d           \@           |  ]         |

\rep M          e          d           \@           |            |
\[  M \@  \rep e          d           \@           |  ]        M|

\rep e          d           \@           |           M|
\[  e \@  \rep d           \@           |  ]       eM|

\rep d           \@           |          eM|
\[  d \@  \rep  \@           |  ]      deM|

\rep  \@           |         deM|
\[   \@  \@ \rep  |  ]  \@  deM|

\@ \rep  |     \@  deM|
|         deM|

• I have enjoyed watching your progress, here. I like the reuse of \R for the delimiters, though I did suspect that you could get away with | instead of one of them (but did't work out the details). I had considered using a braced expression for the stopping condition myself, but couldn't come up with one that I liked, so kudos for getting that to work. Jul 17 '16 at 21:55
• The wrong data is still present in the listing (not the automatic one, but the hand-edited one). I see  \G |Medusa| #1={\def\R...} #2=| #3= Medusa where I expect to see  \G |Medusa| #1=Medusa. Jul 18 '16 at 11:04
• Sorry to misunderstand earlier where the out-of-date #s were, @Davide Cervone, thanks once again! Good excuse to trot out the revised version of that approach, along with a sneak preview of an amusingly short solution, one in which $\small\texttt{\let}$ gets bounced around in a way that could only be discovered accidentally.
– humn
Jul 18 '16 at 12:53
• I have a few puzzle ideas of my own, if I can find time to post them. Jul 18 '16 at 14:02
• Now reflected in the $\sf\small|MathJax~mirror|$, a very invalid solution that looks legitimate and behaves perfectly. Saves 4 characters by getting away with using no |s (illegitimately). You might be amused, @Davide C.
– humn
Jul 20 '16 at 13:18