A while ago I wrotethat multi-variant themes have become the norm - pretty much every popular themethese days is a family of a few dark variants and a light one. And every suchfamily ships its own command for moving between the variants:modus-themes-toggle, ef-themes-select, tokyo-night-select,batppuccin-select and so on.What I had completely missed is that Emacs has had a built-in command for thissince Emacs 29 - theme-choose-variant. I found out about it only recently, whenI tried it with my ownTokyo Night theme and got thisin return:Theme `tokyo-night' does not have any known variantsFour variants, and Emacs knows about none of them. Let’s see why.The commandM-x theme-choose-variant looks at the theme that’s currently enabled andswitches to another theme from the same family. If there’s only one othervariant it just switches to it, otherwise it asks you which one you want. Thecommand is also available as toggle-theme, which is a much better name for thetwo-variant case.You don’t need to install anything to try it, as a few of the themes that shipwith Emacs come in pairs - tango and tango-dark, leuven and leuven-dark,tsdh-light and tsdh-dark:(load-theme 'leuven t)M-x toggle-theme will take you to leuven-dark, and another M-x toggle-themewill bring you back to leuven. The old theme gets disabled before the new oneis loaded, so you don’t end up with one theme stacked on top of the other. (Iwrote about that problem inClean Unloading of Emacs Themes)The Modus themes show the other side of the command. There are eight of them inEmacs these days, so with modus-operandi enabled you won’t get a toggle, but aprompt listing the other seven. And a theme that’s on its own, like wombat,gives you the same error I got:Theme `wombat' does not have any known variantsHow themes opt inHow does Emacs know that leuven and leuven-dark belong together? It doesn’tguess from the names. Emacs 29 taught deftheme to accept a property listafter the docstring, and the theme has to say what family it’s a part of:(deftheme leuven-dark "Face colors with a dark background." :background-mode 'dark :family 'leuven :kind 'color-scheme) :family is what ties the variants together. Themes with the same family arevariants of each other. :kind should be color-scheme for a regular color theme. Themes that don’tset it are treated as color schemes for the time being. :background-mode is light or dark. Nothing uses it yet, but the manualasks theme authors to set it anyway.wombat sets a kind and a background mode, but no family. My themes didn’t setanything at all - their deftheme forms are just a name and a docstring.The autoload catchAdding the properties to deftheme is not enough, though. They are stored on thetheme’s symbol when the theme file gets evaluated, and nobody has evaluated thefiles of the variants you haven’t loaded yet. Emacs has to know the propertiesof every variant before any of them is loaded.The built-in themes solve this with a special cookie:;;;###theme-autoload(deftheme leuven-dark ...When Emacs is built, the properties of every theme marked this way areextracted into a file named theme-loaddefs.el, that’s loaded at startup:(put 'leuven-dark 'theme-properties (list :background-mode 'dark :family 'leuven :kind 'color-scheme))That cookie means nothing in a package you’ve installed from MELPA, though. Athird-party theme has to use the regular ;;;###autoload cookie in front of itsdeftheme. Starting with Emacs 29 the autoload generator knows about defthemeand extracts only the properties from it, so you end up with the same put formin the package’s autoloads file.Without this the command will find only the variants that you’ve already loadedin the current Emacs session, which for most people means none of them:`foo-dark' has no variantsWho supports this?I went over the source of 60 or so popular themes to see how many of them havepicked this up, and the short answer is “very few”. Protesilaos’s themes (Modus,Ef, Standard, Doric) declare a family, and so do doom-themes and a handful ofsmaller packages. Pretty much everyone else declares nothing, including bigmulti-variant families like Solarized, Gruvbox and Kanagawa.What surprised me more is that I didn’t find a single third-party theme thatautoloads its properties. As far as I can tell that leaves the built-in commandblind to the variants you haven’t loaded yet, which is probably why the themesthat did the work kept their own commands anyway. Modus, for instance, quietlyloads its themes (without enabling them) to learn their properties.A few rough edgesThe command is simple, and it shows: It toggles only when the family has exactly two themes. Tokyo Night has four,so I’d get a prompt every time. There’s no way to say “these two are the onesI switch between”. It doesn’t know what’s light and what’s dark, because nothing reads:background-mode. You can’t ask it for “the light one”. It refuses to work if you have more than one theme enabled. It loads the new variant with a plain load-theme, so for a third-party themeyou haven’t marked as safe you’ll get the usual “Loading a theme can run Lispcode” question.That last one is the reason modus-themes-toggle is still around. Protesilaosconsidered dropping it in favor of toggle-theme, and decided against itbecause of the prompts.Also keep in mind that this is a command you invoke by hand. If you want Emacsto follow the appearance of your OS or the time of the day, seeAutomatic Light/Dark Theme Switching.What about older Emacs versions?The good news is that the properties are harmless there. deftheme has beenignoring any extra arguments for ages (I checked all the way back to Emacs 24),so a theme that declares a family will load just fine on Emacs 27 or 28.That’s about all the good news. theme-choose-variant doesn’t exist beforeEmacs 29, so the users of the older versions get nothing out of theproperties. The autoload trick doesn’t translate either - the old autoloadgenerator doesn’t know about deftheme, so it copies the whole form into theautoloads file and the theme gets declared at startup.Both Tokyo Night andBatppuccin support Emacs 27.1,which means I can’t rely on the built-in command and I need one of my ownanyway. It’s not much code:(defconst tokyo-night--variants '(tokyo-night tokyo-night-storm tokyo-night-moon tokyo-night-day));;;###autoload(defun tokyo-night-select () "Select and load a Tokyo theme variant interactively." (interactive) (let* ((names (mapcar #'symbol-name tokyo-night--variants)) (choice (intern (completing-read "Tokyo theme: " names nil t)))) (mapc #'disable-theme tokyo-night--variants) (load-theme choice t) (setq tokyo-night--current choice) (run-hook-with-args 'tokyo-night-after-load-hook choice)))The theme knows its own variants, so there are no properties to autoload and notheme directories to scan. It works the same on every Emacs version, it doesn’task you to confirm anything, and it can do things that only make sense for thistheme, like running a hook after the switch. That’s why I think theme authorswho still support older Emacs versions shouldn’t build ontheme-choose-variant for now. Ship your own command, as most themes alreadydo.The two approaches don’t exclude each other, of course. Declaring a family costsa few lines per variant, and it makes the theme play nice with the built-incommand on Emacs 29+. I’ll be adding the properties to my themes, and their owncommands are staying.Do you use theme-choose-variant? Did you even know it existed? I’d love tohear about it in the comments!That’s all I have for you today. Keep hacking!