WARNING   $ \color{red}{\boxed{\boxed{ \sf ~~{ \large N \, S \, F \, P \rlap{\small P\&CG~SE} }~~~ \huge\raise-.2ex\strut }}} \kern2em $   NOT SUITABLE FOR Programming Puzzles & Code Golf SE

Ain’t it so true that the best MathJax hacks are vanishingly modest?   Not at this puzzle!

Let’s find the shortest answer that is pure MathJax and unabashedly bares all.

•   Begins with the line   $$\require{begingroup}\begingroup   (safety first)

•   Has no line longer than 77 characters

•   Renders entirely in a single size of MathJax’s typewriter font ( $\small \texttt{\\tt}~$ or $\small~\texttt{\\texttt{...}}$ )

•   Lays out and reads exactly the same when rendered and when shown in the edit frame,
  like a quine, including the first and last lines specified here as well as alignment and spacing

•   Spells out its own length, somewhere, formatted like $\small~\texttt{LENGTH = 123}~$
  with space on the left and right of $ \small \tt LENGTH $ and $ \small \tt 123 $ each

•   Ends with the line   \endgroup$$   (again, for safe MathJax)

Spaces and line breaks are encouraged for readability, so they do not count in measuring length. Remember that $ \small \texttt {\tt} $ produces the wrong font for some symbols, such as $ \small ~\tt \{ ~ \} ~ $ braces, which should look like $ \small ~\texttt { { } }~ $ as produced by $ \small ~\texttt{\texttt{ { } }} $ .

Admittedly, the audience for this puzzle is small. Please share any answer that even almost works. No way to spoilerize multi-line MathJax here but who cares, this puzzle is about letting it all hang out.

NOTE ☆ Before posting an answer, be sure to test it on a freshly loaded browser page ☆ Might also need to reload the page while editing, as inadvertent indiscretions in one edit can pervert MathJax results during later edits ☆

MathJax linx   (feel free to add)
MathJax reflex (puzzle)
Starring MathJax (puzzle)
Defining macros in MathJax
$\sf\scriptsize \raise2mu( L \raise4mu{\tiny A} \kern2mu \raise2mu) {\small T} \kern-1mu \raise-4mu E \kern2mu X$ commands in MathJax
$\sf\scriptsize {\small T} \kern-1mu \raise-4mu E \kern2mu X$ commands available in MathJax
MathJax reference at Mathematics Meta
MathJax questions at Meta SE | Physics Meta | Puzzling Meta | Mathematics Meta

  • 1
    $\begingroup$ Not MathJax specifically, but there's a question on TeX quines here. $\endgroup$
    – f''
    Jul 12, 2016 at 13:13
  • 2
    $\begingroup$ This seems to be a MathJax promoting tour :c) I guess you have a 77 chars solution, right? $\endgroup$
    – BmyGuest
    Jul 12, 2016 at 14:53
  • $\begingroup$ Looks like MathJax has to go in completely different directions than the solutions at (La)TeX SE.Those approaches rely on unavailable features (unless some can be $\small\texttt{\require{}}$d?). Far as I know, MathJax has to use $\small\texttt{\begin{array}}$ or $\small\texttt{\begin{align}}$ just to simulate a text margin. $\endgroup$
    – humn
    Jul 13, 2016 at 9:54
  • $\begingroup$ MathJax can also produce a straight margin with $\small \texttt{\matrix{...}}$ (or $\small \texttt{\begin{matrix}}$) and $\small \texttt{\hfil}$, as in Davide Cervone's solution, or by right-padding with $\small \texttt{ \phantom{...}}$. Beware, though, $n$ consecutive spaces do not seem to be rendered $n$ times as wide as a single space. $\endgroup$
    – humn
    Jul 14, 2016 at 6:31

2 Answers 2


New Answer

Here's a shorter version that improves on my earlier answer (below). One of the places where there is a lot of redundancy is in the initial definitions that had to be repeated in the \D...\D block for printing. I wondered if it would be possible to have the code for printing also be executed so that it would not have to be duplicated. This gives the following:

$$\require{begingroup}\begingroup \def\<#1:#2>{\matrix{#1#2\T{#1:}#2\E}} \< \def\C#1;{#1} \def\T#1{\text{#1}\hfil\cr\tt} : \C \def\S{\unicode{36}} \tt\S\S\T{\require{begingroup}\begingroup} ; \C \T{\def\<#1:#2>{\matrix{#1#2\T{#1:}#2\E}}} ; \C \def\E{\T>\text\endgroup\S\S\hfil} % LENGTH = 316 ; \C { \def\C#1;{\T{\C #1;}} } \T\< ; > \endgroup$$

Here, we need one definition to handle the printing of duplicates (the \<#1:#2> macro), so this is the only one that needs to be duplicated (along with the \require{begingroup}\begingroup and \endgroup). The #1 from this macro is code to be executed right away (to define the other macros that are needed internally), and it is explicitly turned into a printed line when it is repeated. The other code (#2) gets executed after the #1 definitions are in place and is repeated a second time, after the first invocation changes the definitions of the key macro, \C, which is for code that is executed on the first pass, and printed on the second. The \T macro is for printing a line.

The first call to \C defines \S for the dollars, and prints the opening line. The next \C prints the definition of \<#1:#2> that is the second line. The third \C defines the \E used in \<#1:#2> to print the ending lines (the > and \endgroup$$). The last \C redefines \C to print itself (so that during the second pass, the code prints itself verbatim), and prints the \< line. We have to put the redefinition in braces so that it's ; won't end the \C for that line.

We use \C#1; rather than \C{#1} in order to save a character per line (the opening brace, since spaces don't count against us).

The \tt font is added to each row automatically by the \T macro (and the initial row has it explicitly before the \S\S). The \text macro will pick up the current font, so we save a couple of characters with this trick.

You can reduce the character and line count slightly by moving the \< and > to adjacent lines, shifting the \C lines around a bit, moving the \T from the definition of \<#1:#2> into the last \C, and not using the ; with \C, since there are too few to make that an advantage, at the loss of some readability, to come in at 307 characters and 7 lines.

$$\require{begingroup}\begingroup \def\<#1:#2>{\matrix{#1#2{#1:}#2\E}} \< \def\C#1{#1} \def\T#1{\text{#1}\hfil\cr\tt} : \C{ \def\S{\unicode{36}} \tt\S\S\T{\require{begingroup}\begingroup} } \C{ \T{\def\<#1:#2>{\matrix{#1#2{#1:}#2\E}} \<} % LENGTH = 307 } \C{ \def\E{>\text\endgroup\S\S\hfil} \def\C#1{\T{\C{#1}}} \T } > \endgroup$$

I think that is as far as I will go.

Initial Answer

Here's a version that is slightly shorter than humn's, and uses a somewhat different approach to the duplication. It uses \matrix{} rather than \begin{array}...\end{array} to save space, and \hfil to make the lines align flush left. Since humn didn't count spaces or line breaks in the character count, I left them out of mine as well.

$$\require{begingroup}\begingroup % LENGTH = 463 \def\S{\tt\unicode{36}} \def\N{\hfil\cr} \def\E{\texttt{\endgroup}} \def\R#1{\texttt{#1}\N} \def\T#1{\R{\L{#1}}} \let\L=\R \def\D#1\D{\matrix{\S\S#1 \R\D \let\L=\T #1 \R\D \E\S\S\N}} \D \L{\require{begingroup}\begingroup} \L{% LENGTH = 463 } \L{\def\S{\tt\unicode{36}} \def\N{\hfil\cr} \def\E{\texttt{\endgroup}}} \L{\def\R#1{\texttt{#1}\N} \def\T#1{\R{\L{#1}}} \let\L=\R} \L{\def\D#1\D{\matrix{\S\S#1 \R\D \let\L=\T #1 \R\D \E\S\S\N}}} \D \endgroup$$

  • 1
    $\begingroup$ Sorry! A newline sneaked in at the end of the file before I with the character count. You are correct, it should be 463. I have edited the post. I'm sorry that the value has changed, as I liked the fact that yours was 644 and mine 464. Oh well, can't have everything. Thanks for the fun puzzle! $\endgroup$ Jul 13, 2016 at 20:41
  • 1
    $\begingroup$ Many thanx for MathJax, Davide Cervone! Just saw that you are one of the forces behind it. Glad that you somehow noticed this puzzle. MathJax is one of my very favorite discoveries here $\endgroup$
    – humn
    Jul 13, 2016 at 20:43
  • 1
    $\begingroup$ Thanks! I have an alert that informs me of posts that talk about MathJax, so I check on those. Yours looked like fun, so I thought Id' try my hand at it. $\endgroup$ Jul 13, 2016 at 20:47
  • 1
    $\begingroup$ I've had no luck getting my count to a corresponding 643, but managed to bloat yours to an honest 464 by removing = after $\small\texttt{\let}$ in 4 places and by changing $\small\texttt{\D}$ to $\small\texttt{\HO}$ in the 5 places where it so delightfully(!) represents a \Delimiter. For instance, line 5 would be \def\D#1\HO{\matrix{\S\S#1 \R\D \let\L\T #1 \R\HO \E\S\S\N}} and has been tested. (So your solution is as good as 459 already, by the way.) I had been pondering an accretion solution like yours but see no need anymore and feel privileged to learn from the source $\endgroup$
    – humn
    Jul 13, 2016 at 23:46
  • 1
    $\begingroup$ Thanks for your continued informative comments. I'm glad you are enjoying playing with my solution. I have posted a new answer that reduces the length to 316 characters, slightly less than half your original answer, and 10 lines (though it would be possible to move the \< and > into adjacent lines, with a savings of one character, but I didn't think the change was worth the loss of readability). In any case see what you think. I'm not going to try to reduce this further, but have enjoyed the puzzle thus far. $\endgroup$ Jul 14, 2016 at 12:33

Plagiarized addition

Nothing original here beyond what was initially in Davide Cervone's solution. This only contorts it for the sake of ever fewer lines (10) by doing the obvious, in tucking the comment into another line, and by doing the deranged, in using the terminal $\small\texttt{\endgroup}$ as a delimiter.

$$\require{begingroup}\begingroup \def \S {\tt\unicode{36}} \def \E {\texttt\endgroup} % LENGTH = 471 \def \R #1{\texttt{#1}\hfil\cr} \def \T #1{\R{\L{#1}}} \let \L \R \small \def \D #1\endgroup{\matrix{\S\S#1 \R\D\let\L\T #1 \E\S\S\hfil}\endgroup} \D \L{\require{begingroup}\begingroup} \L{\def \S {\tt\unicode{36}} \def \E {\texttt\endgroup} % LENGTH = 471} \L{\def \R #1{\texttt{#1}\hfil\cr} \def \T #1{\R{\L{#1}}} \let \L \R \small} \L{\def \D #1\endgroup{\matrix{\S\S#1 \R\D\let\L\T #1 \E\S\S\hfil}\endgroup}} \endgroup$$

The width of these lines requires the use of $\small\texttt{\small} $ font in order to maintain a straight left margin. Safe MathJax is practiced by including $\small \texttt {\endgroup} $ within $ \small~\texttt {\def\D...\endgroup{...\endgroup}}~$ because the final $ \small \texttt {\endgroup} $ is swallowed as a delimiter by $ \small~\texttt {\D...\endgroup} $.

Initial answer

This was an introductory example of a valid solution that expected to be bettered and offered usable parts for harvest. I didn't actually have a solution when originally posing the puzzle, just assumed that many different ones are possible.

$$\require{begingroup}\begingroup \def \L #1{\texttt{#1}\cr} \def \M #1{\L{{{#1}}}} \def \X #1#2#3#4#5#6#7{ #1{#2}#1{#3}#1{#4}#1{#5}#1{#6}#1{#7} } \def \U #1#2#3#4#5#6{\S\S \X\L#1#2#3#4#5#6 \L\U \X\M#1#2#3#4#5#6} \def \D #1#2{\L\D\M#1\M#2\L#1\texttt#2\S\S} \def \S {\tt\unicode{36}} \begin{array}{ LENGTH = 644 } \U {{\require{begingroup}\begingroup}} {{\def \L #1{\texttt{#1}\cr} \def \M #1{\L{{{#1}}}}}} {{\def \X #1#2#3#4#5#6#7{ #1{#2}#1{#3}#1{#4}#1{#5}#1{#6}#1{#7} }}} {{\def \U #1#2#3#4#5#6{\S\S \X\L#1#2#3#4#5#6 \L\U \X\M#1#2#3#4#5#6}}} {{\def \D #1#2{\L\D\M#1\M#2\L#1\texttt#2\S\S} \def \S {\tt\unicode{36}}}} {{\begin{array}{ LENGTH = 644 }}} \D {{\end{array}}} {{\endgroup}} \end{array} \endgroup$$

  • $\begingroup$ Oddly, on my iPad app, The answer only displays down to 'LENGTH = 644 }' .... $\endgroup$
    – BmyGuest
    Jul 13, 2016 at 19:59
  • $\begingroup$ Wonder if this puzzle is NSFiP too. Does the other solution display correctly? $\endgroup$
    – humn
    Jul 13, 2016 at 20:01
  • $\begingroup$ Oh, it does display correctly (with font and all) up to that line, but then just stops with the upper half line of '\u' displayed... $\endgroup$
    – BmyGuest
    Jul 13, 2016 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.