# 2015-05-23 Math examples in TeX, part II

Some time ago, I wrote about a macro to typeset examples of math typesetting in LaTeX. I mentioned then that there’s a bit more to the real code I’m using. So, here it is, in hope that someone might find this inspiring and/or learn something from the methods I used.

First of all, I wanted to be able to show examples of displayed equations, in the same format (code to the left, result to the right). All this is about how not to duplicate code, so I obviously did not want to copy-paste (ekhm, kill-yank) the \@mathdemo macro. Here’s the goal: make it general enough so that it’s possible to write \dmathdemo with minimal code duplication.

This is simple. All we need to do is put \displaystyle in front of the equation. And while we’re at it, we might want to demonstrate various math delimiters (like $...$ and $$...$$), so I also wanted \rmathdemo (r’ like raw’), in which I would supply the math delimiters, too. Here’s the code (don’t forget to put \makeatletter/\makeatother around it!):

\def\mathdemo{%
\bgroup
\let\do\@makeother
\dospecials
\global\let\@atbeginmathdemo=$$\global\let\@atendmathdemo=$$
\@mathdemo
}
\def\rmathdemo{% raw
\bgroup
\let\do\@makeother
\dospecials
\global\let\@atbeginmathdemo=\relax
\global\let\@atendmathdemo=\relax
\@mathdemo
}
\def\dmathdemo{%
\bgroup
\let\do\@makeother
\dospecials
\global\def\@atbeginmathdemo{$$\displaystyle} \global\let\@atendmathdemo=$$
\@mathdemo
}
\def\@mathdemo#1{%
\egroup
\par\smallskip\noindent
\makebox[0.48\textwidth]{\texttt{#1}}\hfill
\framebox[0.48\textwidth]{%
\@atbeginmathdemo\scantokens{#1}\@atendmathdemo
}\par\smallskip
}


Here’s how you use these macros.

\mathdemo\frac{1}{2}(a_1+a_2)^2
\dmathdemo\frac{1}{2}(a_1+a_2)^2
\rmathdemo$\frac{1}{2}(a_1+a_2)^2$


and here’s the result:

Next time, I will show a few finishing touches – but for today that’s it. Happy TeXing!

Komentarze na tej stronie

# 2015-05-16 which-function and LaTeX

There is a very nice minor Emacs mode for programmers: which-function-mode. It is especially useful if you are a poor programmer who writes functions longer than 20 lines, or you use a poor programming language which makes you write such functions. (OK, it was a bad joke. OTOH, there seems to be a grain of truth in it…)

The mode puts the name of the current function in the modeline. One interesting thing about which-function-mode is that it is based on imenu. The comments in the source claim that this dependency should be removed. I’m not sure why: imenu seems to be a fine tool, already capable of extracting the information about where particular functions are located in the source. One possible problem is that (probably due to imenu dependence), which-function-mode treats places located after a function (but not inside another one) as belonging to that function. (Also, I’m not sure what would happen in case of nested functions.) This seems not to be a deal-breaker, though.

While imenu deserves a blog post on its own, I’d like to focus on one particular issue with which-function-mode. While I really like the fact that it works in LaTeX files (thanks to AUCTeX’s support for imenu), I did not appreciate that the section and subsection names had spaces in front of them. Even though it does make sense with imenu (the indentation corresponds to the sectioning level), it looked terrible in the modeline. The remedy turned out to be straightforward, though:

(add-hook 'TeX-mode-hook '(lambda ()
(make-local-variable 'which-func-cleanup-function)
(setq which-func-cleanup-function #'string-trim-left)))


One curious thing someone might ask is: how do I know that the function which removes leading blanks from a string is called string-trim-left? Well, the answer is quite obvious for anyone who uses the excellent Emacs help system: apropos. It is enough to say M-x apropos RET trim RET. Depending on your Emacs config, it may list from a couple to a dozen or so functions. (Rather surprisingly, emacs -Q does not load subr-x by default; I’m not sure which part of my init.el loads it, but I’m convinced it gets loaded at some point in any practical Emacs configuration.) BTW, I didn’t use the apropos command, but the search facility provided by Icicles. What I actually did was C-h f trim S-TAB, but this is more or less equivalent to apropos, only more convenient: it lets the user find the docs in less keystrokes, displays the first line of the docstring in the modeline when browsing through the items, and excludes things other than function (like variables).

One more thing which is worth remembering is that imenu‘s “Rescan buffer” option (visible in the completion menu after pressing TAB) is not trivial to launch programmatically. Since I could not find a way to do it, I asked a relevant question on Emacs.SE, and got two reasonable answers. (Also, I filed a bug as per Stefan’s suggestion.)

Komentarze na tej stronie

# 2015-05-09 Two quotes from John McCarthy

I’ve been rather busy lately, so here’s only a short post today. I finally read (well, skimmed at least) the original McCarthy's paper on Lisp, and I cannot resist giving two quotes from it (well, to be precise, the second one is the author’s later comment). BTW, I highly recommend this paper, as well as Paul Graham’s The roots of Lisp, which can be seen as a commentary on McCarthy’s work.

The first quote is this:

The prohibition against circular list structures is essentially a prohibition against an expression being a subexpression of itself. Such an expression could not exist on paper in a world with our topology.

(Remember that McCarthy was a student of Solomon Lefschetz, famous among others for his fixed point theorem, a result from algebraic topology.)

A few pages later, the author writes this:

Nothing happens until the program runs out of free storage. When a free register is wanted, and there is none left on the free-storage list, a reclamation cycle starts.

and comments the above in a footnote:

We already called this process “garbage collection”, but I guess I chickened out of using it in the paper—or else the Research Laboratory of Electronics grammar ladies wouldn’t let me.

I can’t decide which one is more cute…

Komentarze na tej stronie

Więcej...

(Więcej means More in Polish; click it to see older entries.)