Mathjax enabler

Tuesday, April 8, 2025

Locally finding a latex package defining a given symbol

I am not an advanced latex user, and I'm often questioning in which package a given known command is defined. For example, where are the symbols \sfrac, \mathbb and \substack defined? I believe answering this question is something natural for accustomed latex users. For me, I keep forgetting that \mathbb is defined in the package amssymb.

I wrote a small command line utility in Bash to try to find this for me. It scans through my local latex installation files and attempts to find where a given command is defined by looking for lines with likely command definitions. This search is done in the internal sty, tex and cls files.

To do this, the script employs a simple heuristic. Say you want to find where \sfrac is defined. The script looks for lines containing one of "def", "decl", "cmd", "command", "new" somewhere before the command name, and it takes this as good enough for "likely command definition". To search a file.sty for possible definitions of \sfrac, we'd do:

grep -Ei "(def|decl|cmd|command|new).*\\\\sfrac[^a-zA-Z]" file.sty | grep "\\\\sfrac[^a-zA-Z]"

This is a "strict" mode, in which we're less willing to accept false positives. If you don't exactly remember the capitalization of the command and/or want to allow for prefix/suffix searches, you could consider a looser search: 

grep -Ei "(def|decl|cmd|command|new).*sfrac" file.sty

Variations of this are possible, of course. I'm going for the more strict search myself. A more interesting direction would be improving the search heuristics. For example, we could count how many '\' there are before the searched command. You should get the one for the command, and another one for its respective \def or \newcommand, and no more (right?). It seems like a valid heuristic to look at. The adapted grep search would be:

grep -Ei "(command|cmd|new|def|decl)[^\\\\]*\\\\sfrac[^a-zA-Z]" file.sty | grep "\\\\sfrac[^a-zA-Z]"

Latex offers great flexibility and dynamicity for defining new commands, therefore such heuristics based on simple source analysis won't fully work, but they help with many common simpler cases.

To turn this type of grep search into a script, we can simply loop through the .tex, .sty and .cls files within the latex distribution, and apply the filter at each file. This batching can be done through find.

The current version of the script I'm running is in https://pastebin.com/FSdQtfVt (raw: https://pastebin.com/raw/FSdQtfVt). I intend to make a small github repository for this later on. However, first, I'll see if I can find already made tools that do a better job at this. 

This came about while I was trying to remember where \mathbb is defined. Of course, a simple web search solves this, but in my third search (in different days, hehe), and after searching for many commands (and not remembering where they where), I thought enough is enough and decided to look for a tool to do this. Not initially finding one and having this idea, I decided to write this script.

Here is an example of its usage. I call the script tex-which and I've set it up so that the prefixing '\' is always automatically added. If I search for \substack, I get:
pedro@pedro-Inspiron-15-3520:~$ tex-which substack
Looking for \substack in /usr/share/texlive/texmf-dist/tex/ ... (done)

Found possible symbol definition for \substack in files:
amsmath-2018-12-01.sty  amsmath.sty

Full results:
amsmath-2018-12-01.sty in latex-dev/amsmath
        \newcommand{\substack}[1]{\subarray{c}#1\endsubarray}
amsmath.sty in latex-dev/amsmath
        \DeclareRobustCommand{\substack}[1]{\subarray{c}#1\endsubarray}
amsmath-2018-12-01.sty in latex/amsmath
        \newcommand{\substack}[1]{\subarray{c}#1\endsubarray}
amsmath.sty in latex/amsmath
        \DeclareRobustCommand{\substack}[1]{\subarray{c}#1\endsubarray}

Tuesday, March 25, 2025

Setting up Catch2 (v3) for C++ unit testing on Ubuntu (24.10)

In Ubuntu, you can install Catch2 from the command line with ``sudo apt-get install catch2`` and it’ll put two library files into your /usr/lib: libCatch2.a and libCatch2Main.a. Library headers will also be put in the appropriate places.

Each test file you have (a something_test.cpp for example) will be built into its own program by linking against the two provided static libraries, for example: ``g++ something_test.cpp -o something_test -lCatch2 -lCatch2Main``.Within its source, such a test file must also include the catch2/catch_all.hpp installed header.

See:


The first page is a basic tutorial on how to use the library. The second mentions issues having to do with migrating to newer versions. It seems like the library was headers only in the past, which contributed to slower compilation times. Now, they moved into a static library (.a) model.


Monday, September 12, 2022

First post

Hi. I'm Pedro Henrique Antunes de Oliveira (/u/phao on reddit and @pedrohen1989 on twitter) and I'm currently a Math PhD student at UFPR in Brazil. I intend to talk about Mathematics in this blog, specially things I like related to numerical analysis and computational mathematics in general.


The first piece of goods news is that Mathjax integration worked just great, thanks to this very simple tutorial https://jesuscapistran.com/2020/01/12/how-to-add-math-on-your-blog-mathjax-blogger/.

\[ F(1) - F(0) = \int_0^1 f(x) dx. \]

Locally finding a latex package defining a given symbol

I am not an advanced latex user, and I'm often questioning in which package a given known command is defined. For example, where are t...