diff --git a/content.org b/content.org index 969e6fe..1583abc 100644 --- a/content.org +++ b/content.org @@ -3134,6 +3134,162 @@ that is not strictly necessary anymore. You might want to setup a separate email account for this purpose altogether if you're not comfortable with having a (potentially buggy) python script mucking around in your emails. +*** DONE Subfigures in RevTex :ATTACH: +CLOSED: [2025-02-15 Sat 19:32] +:PROPERTIES: +:EXPORT_FILE_NAME: subfigures-in-revtex +:ID: d036f9e0-a55d-41a6-a884-8c377b50caf4 +:END: + +I'm in the process of writing a paper at the moment (yay!). Merrily I +add a formula here a plot there; but my supervisor zeroes in +immediately on a two-panel figure with sub figures called "a" and "b" +saying: +#+begin_quote +The sub figure labels go in the top-left corner. You've read papers +before, haven't you. +#+end_quote +Having indeed read papers before, I must have neglected to pay proper +attention to sub-figure labels -- an inexcusable blunder -- so this +was news to me. Unwilling to fix the issues by monkeying around in +inkscape, I turned to stack overflow. We have the pleasure to be using +the [[https://www.ctan.org/tex-archive/macros/latex/contrib/revtex][RevTeX]] document class and annoyingly this class happens to be +incompatible with both the [[https://ctan.org/pkg/subcaption?lang=en][subcaption]] package and the part of [[https://ctan.org/pkg/subfloat][subfloat]] +that controls where the captions go. The aforementioned packages are +"the way" to do sub figures and most other solutions I found involved +monkeying around with minipages. + +So that's what I did; but I wanted to do properly so I'd be able to +give the sub figures referenceable labels. I came up with a +surprisingly short piece of hacked-together LaTeX package which we +will now walk through. If you're impatient, you can just grab the [[/static/tex/poormanssubfig.sty][sty]] +file and jump ahead to the usage example. + +We begin by declaring the package. +#+begin_src latex :tangle static/tex/poormanssubfig.sty + \NeedsTeXFormat{LaTeX2e} + \ProvidesPackage{poormanssubfig}[2025/02/14 A subfigure environment + with working labels in the top-left corner that is compatible with revtex.] +#+end_src +Then, we create a new counter for the subfigures to be able to +generate use labels to reference them. +#+begin_src latex :tangle site/static/tex/poormanssubfig.sty + \newcounter{subfigure} +#+end_src +Now there comes the meat! The ~multifig~ environment resets the +subfigure counter[fn::Which could also have been accomplished by an +argument to ~\newcounter~.], and redefines the ~\thesubfigure~ macro to +display references to the subfigure as ~[Figure Number] ([figure +sublabel])~. The environment temporarily increases the Figure counter +so that ~\thefigure~ prints the right number. The three commands at the +top of the below snipped allow the user to overwrite the way the label +is printed and formatted. +#+begin_src latex :tangle site/static/tex/poormanssubfig.sty + \newcommand{\subfiglabelformat}[1]{\raggedright{#1}} + \newcommand{\subfiglabelstyle}[1]{\alph{#1}} + \newcommand{\subfiglabelsize}{\small} + \newenvironment{multifig}% + {\addtocounter{figure}{1}% + \setcounter{subfigure}{0}% + \renewcommand\thesubfigure{\thefigure~(\subfiglabelstyle{subfigure})}% + } + {\addtocounter{figure}{-1}} +#+end_src + +Now the hackery begins. We define an internal variables ~\if@insubfig~ +whose use will become clear later. The subfigure environment takes two +arguments, the first one being the width of the subfigure and the +second being an optional label string (it's also where you put +~\label{[label]}~). The environment then creates a minipage with the +appropriate width and prints the label of the figure in the top-left +corner. +#+begin_src latex :tangle site/static/tex/poormanssubfig.sty + \makeatletter + \ExplSyntaxOn + \newif\if@insubfig + \NewDocumentEnvironment{subfigure}{mO{}}{% + \@insubfigtrue% + \refstepcounter{subfigure}% + \begin{minipage}{#1}% + \subfiglabelformat{{\subfiglabelsize (\subfiglabelstyle{subfigure})~#2}}% + } + {\end{minipage}% + \@insubfigfalse% + } + \ExplSyntaxOff +#+end_src + +This final bit is truly horrendous. I wanted to be able to refer to +the subfigures in the figure caption using their labels, but a +standard ~\ref{[label]}~ would print the figure number, too. So I stole +some code from the [[https://ctan.org/tex-archive/macros/latex/contrib/cleveref][cleveref]] package to redefine the ~\label~ macro to +write a new label format (with the label name ~[youlabel]@multifig~) to +the auxiliary (=.aux)= file upon compilation. LaTeX reads the auxiliary +file on the second pass and evaluates the ~\newlabel{[label]}~ macros +therein. These tell latex which text to print when we use the macro +~\ref{[label]}~. In our case this is just the sufigure counter value, +without the figure number. I define a new reference command ~\subref~ to expand ~\ref~ but +append ~@multifig~ to its argument, so that the text we stored using +~\newlabel~ is used. These extra labels will only be generated for +subfigures thanks to ~\if@insubfig~. +#+begin_src latex :tangle site/static/tex/poormanssubfig.sty + \let\multifig@old@label\label% + \def\label#1{% + \multifig@old@label{#1}% + \if@insubfig% + \@bsphack% + \protected@write\@auxout{}% + {\string\newlabel{#1@multifig}{{(\subfiglabelstyle{subfigure})}% + {\thepage}% + {\@currentlabelname}% + {\@currentHref}{}% + }}% + \@esphack% + \fi + }% + \def\subref#1{\ref{#1@multifig}} + \makeatother +#+end_src + +With these details out of the way, let's get to the part that you +actually wanted to read about[fn::Who are you dear reader? Shoot me an +email!]: The package is used in a similar way to -- who'd have thought +it -- the subfigure package, but puts your label it the top left +corner -- where it ought to be! Just put the ~.sty~ file somewhere ~TeX~ +will find it. For global installation, you can put it in your +~$TEXMFHOME/tex/latex~ folder. For usage with a specific project, you +can dump it next to your ~.tex~ file. And now, behold the below usage example. +#+begin_src latex +\begin{figure*} + \begin{multifig} + \begin{subfigure}{\columnwidth}{\label{subfig:descriptive-label}Some + text label.} + \begin{center} + \includegraphics[width=.8\columnwidth]{example-image-a} + \end{center} + \end{subfigure} + \begin{subfigure}{\columnwidth}{\label{subfig:another-descriptive-label}Another + text label.} + \begin{center} + \includegraphics[width=.8\columnwidth]{example-image-b} + \end{center} + \end{subfigure} + \end{multifig} + \caption{\label{fig:beautiful-figures}As the discerning eye may have + spied, the figures \subref{subfig:descriptive-label} and + \subref{subfig:another-descriptive-label} have the potential to + rock the foundations of society.} +\end{figure*} +Total nonsense, but I couldn't think of anything better to demonstrate +Figure \ref{subfig:descriptive-label} and \ref{subfig:another-descriptive-label}. + +This produces the below result... +#+end_src +[[attachment:screen:b4ea3f2a-f6a3-467d-a9ae-56bb5f0b9084.png]] +... and references in the text look like +[[attachment:screen:fef71aa0-4836-4777-8c42-1118c37c26b9.png]] + +Cheers! ** Uncategorized :@Uncategorized: *** Neuseeland Restauration @@ -3501,6 +3657,7 @@ und Mantelstromtriebwerke. :PROPERTIES: :EXPORT_FILE_NAME: tired-of-downloading-the-same-paper-over-and-over-clickable-pdf-links-in-the-natbib-revtex-bibiography-dot :END: + ** Canada :@Canada: *** DONE Entscheidung CLOSED: [2022-08-28 Sun 17:53] @@ -4962,10 +5119,6 @@ chaotisch. * Local Vars - -* Footnotes - -[fn:6] # Local Variables: # eval: (org-hugo-auto-export-mode) # org-download-image-dir: "./static/images" diff --git a/shell.nix b/shell.nix index a7cbbe2..2affd95 100644 --- a/shell.nix +++ b/shell.nix @@ -1,4 +1,4 @@ -{ pkgs ? import { } }: -pkgs.mkShell { - nativeBuildInputs = with pkgs; [ hugo ]; +{ pkgs ? import {} }: + pkgs.mkShell { + nativeBuildInputs = with pkgs.buildPackages; [ hugo nodejs ]; } diff --git a/site/config.toml b/site/config.toml index c119021..54dddc6 100644 --- a/site/config.toml +++ b/site/config.toml @@ -51,3 +51,8 @@ baseURL = "https://protagon.space" [permalinks] posts = "/:year/:month/:title/" + +[markup] + [markup.goldmark] + [markup.goldmark.renderer] + unsafe = true diff --git a/site/content/posts/subfigures-in-revtex.md b/site/content/posts/subfigures-in-revtex.md new file mode 100644 index 0000000..e41f85a --- /dev/null +++ b/site/content/posts/subfigures-in-revtex.md @@ -0,0 +1,170 @@ ++++ +title = "Subfigures in RevTex" +author = ["Valentin Boettcher"] +date = 2025-02-15T19:32:00-05:00 +tags = ["ATTACH"] +categories = ["Hacks"] +draft = false ++++ + +I'm in the process of writing a paper at the moment (yay!). Merrily I +add a formula here a plot there; but my supervisor zeroes in +immediately on a two-panel figure with sub figures called "a" and "b" +saying: + +> The sub figure labels go in the top-left corner. You've read papers +> before, haven't you. + +Having indeed read papers before, I must have neglected to pay proper +attention to sub-figure labels -- an inexcusable blunder -- so this +was news to me. Unwilling to fix the issues by monkeying around in +inkscape, I turned to stack overflow. We have the pleasure to be using +the [RevTeX](https://www.ctan.org/tex-archive/macros/latex/contrib/revtex) document class and annoyingly this class happens to be +incompatible with both the [subcaption](https://ctan.org/pkg/subcaption?lang=en) package and the part of [subfloat](https://ctan.org/pkg/subfloat) +that controls where the captions go. The aforementioned packages are +"the way" to do sub figures and most other solutions I found involved +monkeying around with minipages. + +So that's what I did; but I wanted to do properly so I'd be able to +give the sub figures referenceable labels. I came up with a +surprisingly short piece of hacked-together LaTeX package which we +will now walk through. If you're impatient, you can just grab the [sty](/static/tex/poormanssubfig.sty) +file and jump ahead to the usage example. + +We begin by declaring the package. + +```latex +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{poormanssubfig}[2025/02/14 A subfigure environment + with working labels in the top-left corner that is compatible with revtex.] +``` + +Then, we create a new counter for the subfigures to be able to +generate use labels to reference them. + +```latex +\newcounter{subfigure} +``` + +Now there comes the meat! The `multifig` environment resets the +subfigure counter[^fn:1], and redefines the `\thesubfigure` macro to +display references to the subfigure as `[Figure Number] ([figure +sublabel])`. The environment temporarily increases the Figure counter +so that `\thefigure` prints the right number. The three commands at the +top of the below snipped allow the user to overwrite the way the label +is printed and formatted. + +```latex +\newcommand{\subfiglabelformat}[1]{\raggedright{#1}} +\newcommand{\subfiglabelstyle}[1]{\alph{#1}} +\newcommand{\subfiglabelsize}{\small} +\newenvironment{multifig}% +{\addtocounter{figure}{1}% + \setcounter{subfigure}{0}% + \renewcommand\thesubfigure{\thefigure~(\subfiglabelstyle{subfigure})}% +} +{\addtocounter{figure}{-1}} +``` + +Now the hackery begins. We define an internal variables `\if@insubfig` +whose use will become clear later. The subfigure environment takes two +arguments, the first one being the width of the subfigure and the +second being an optional label string (it's also where you put +`\label{[label]}`). The environment then creates a minipage with the +appropriate width and prints the label of the figure in the top-left +corner. + +```latex +\makeatletter +\ExplSyntaxOn +\newif\if@insubfig +\NewDocumentEnvironment{subfigure}{mO{}}{% + \@insubfigtrue% + \refstepcounter{subfigure}% + \begin{minipage}{#1}% + \subfiglabelformat{{\subfiglabelsize (\subfiglabelstyle{subfigure})~#2}}% + } + {\end{minipage}% + \@insubfigfalse% +} +\ExplSyntaxOff +``` + +This final bit is truly horrendous. I wanted to be able to refer to +the subfigures in the figure caption using their labels, but a +standard `\ref{[label]}` would print the figure number, too. So I stole +some code from the [cleveref](https://ctan.org/tex-archive/macros/latex/contrib/cleveref) package to redefine the `\label` macro to +write a new label format (with the label name `[youlabel]@multifig`) to +the auxiliary (`.aux)` file upon compilation. LaTeX reads the auxiliary +file on the second pass and evaluates the `\newlabel{[label]}` macros +therein. These tell latex which text to print when we use the macro +`\ref{[label]}`. In our case this is just the sufigure counter value, +without the figure number. I define a new reference command `\subref` to expand `\ref` but +append `@multifig` to its argument, so that the text we stored using +`\newlabel` is used. These extra labels will only be generated for +subfigures thanks to `\if@insubfig`. + +```latex +\let\multifig@old@label\label% +\def\label#1{% + \multifig@old@label{#1}% + \if@insubfig% + \@bsphack% + \protected@write\@auxout{}% + {\string\newlabel{#1@multifig}{{(\subfiglabelstyle{subfigure})}% + {\thepage}% + {\@currentlabelname}% + {\@currentHref}{}% + }}% + \@esphack% + \fi +}% +\def\subref#1{\ref{#1@multifig}} +\makeatother +``` + +With these details out of the way, let's get to the part that you +actually wanted to read about[^fn:2]: The package is used in a similar way to -- who'd have thought +it -- the subfigure package, but puts your label it the top left +corner -- where it ought to be! Just put the `.sty` file somewhere `TeX` +will find it. For global installation, you can put it in your +`$TEXMFHOME/tex/latex` folder. For usage with a specific project, you +can dump it next to your `.tex` file. And now, behold the below usage example. + +```latex +\begin{figure*} + \begin{multifig} + \begin{subfigure}{\columnwidth}{\label{subfig:descriptive-label}Some + text label.} + \begin{center} + \includegraphics[width=.8\columnwidth]{example-image-a} + \end{center} + \end{subfigure} + \begin{subfigure}{\columnwidth}{\label{subfig:another-descriptive-label}Another + text label.} + \begin{center} + \includegraphics[width=.8\columnwidth]{example-image-b} + \end{center} + \end{subfigure} + \end{multifig} + \caption{\label{fig:beautiful-figures}As the discerning eye may have + spied, the figures \subref{subfig:descriptive-label} and + \subref{subfig:another-descriptive-label} have the potential to + rock the foundations of society.} +\end{figure*} +Total nonsense, but I couldn't think of anything better to demonstrate +Figure \ref{subfig:descriptive-label} and \ref{subfig:another-descriptive-label}. + +This produces the below result... +``` + +![](/ox-hugo/screen:b4ea3f2a-f6a3-467d-a9ae-56bb5f0b9084.png) +... and references in the text look like +![](/ox-hugo/screen:fef71aa0-4836-4777-8c42-1118c37c26b9.png) + +Cheers! + +[^fn:1]: Which could also have been accomplished by an + argument to `\newcounter`. +[^fn:2]: Who are you dear reader? Shoot me an + email! diff --git a/site/static/ox-hugo/screen:0a33ca74-2e99-4ed2-9631-a16c652c16bd.png b/site/static/ox-hugo/screen:0a33ca74-2e99-4ed2-9631-a16c652c16bd.png new file mode 100644 index 0000000..643e7f6 Binary files /dev/null and b/site/static/ox-hugo/screen:0a33ca74-2e99-4ed2-9631-a16c652c16bd.png differ diff --git a/site/static/ox-hugo/screen:b4ea3f2a-f6a3-467d-a9ae-56bb5f0b9084.png b/site/static/ox-hugo/screen:b4ea3f2a-f6a3-467d-a9ae-56bb5f0b9084.png new file mode 100644 index 0000000..e851ad9 Binary files /dev/null and b/site/static/ox-hugo/screen:b4ea3f2a-f6a3-467d-a9ae-56bb5f0b9084.png differ diff --git a/site/static/ox-hugo/screen:fef71aa0-4836-4777-8c42-1118c37c26b9.png b/site/static/ox-hugo/screen:fef71aa0-4836-4777-8c42-1118c37c26b9.png new file mode 100644 index 0000000..1e9f70a Binary files /dev/null and b/site/static/ox-hugo/screen:fef71aa0-4836-4777-8c42-1118c37c26b9.png differ diff --git a/site/static/tex/poormanssubfig.sty b/site/static/tex/poormanssubfig.sty new file mode 100644 index 0000000..7f5e32d --- /dev/null +++ b/site/static/tex/poormanssubfig.sty @@ -0,0 +1,42 @@ +\newcounter{subfigure} + +\newcommand{\subfiglabelformat}[1]{\raggedright{#1}} +\newcommand{\subfiglabelstyle}[1]{\alph{#1}} +\newcommand{\subfiglabelsize}{\small} +\newenvironment{multifig}% +{\addtocounter{figure}{1}% + \setcounter{subfigure}{0}% + \renewcommand\thesubfigure{\thefigure~(\subfiglabelstyle{subfigure})}% +} +{\addtocounter{figure}{-1}} + +\makeatletter +\ExplSyntaxOn +\newif\if@insubfig +\NewDocumentEnvironment{subfigure}{mO{}}{% + \@insubfigtrue% + \refstepcounter{subfigure}% + \begin{minipage}{#1}% + \subfiglabelformat{{\subfiglabelsize (\subfiglabelstyle{subfigure})~#2}}% + } + {\end{minipage}% + \@insubfigfalse% +} +\ExplSyntaxOff + +\let\multifig@old@label\label% +\def\label#1{% + \multifig@old@label{#1}% + \if@insubfig% + \@bsphack% + \protected@write\@auxout{}% + {\string\newlabel{#1@multifig}{{(\subfiglabelstyle{subfigure})}% + {\thepage}% + {\@currentlabelname}% + {\@currentHref}{}% + }}% + \@esphack% + \fi +}% +\def\subref#1{\ref{#1@multifig}} +\makeatother diff --git a/static/tex/poormanssubfig.sty b/static/tex/poormanssubfig.sty new file mode 100644 index 0000000..364b7c7 --- /dev/null +++ b/static/tex/poormanssubfig.sty @@ -0,0 +1,3 @@ +\NeedsTeXFormat{LaTeX2e} +\ProvidesPackage{poormanssubfig}[2025/02/14 A subfigure environment + with working labels in the top-left corner that is compatible with revtex.]