
<feed xmlns="http://www.w3.org/2005/Atom">
    <generator>Hugo -- gohugo.io</generator>
    <title>
                yasnippet on
            
        
        The Neo-Babbage Files</title>
        <link href="https://babbagefiles.xyz/tags/yasnippet/atom.xml" rel="self" type="application/atom+xml" /><link href="https://babbagefiles.xyz/tags/yasnippet/"/>
    <updated>2025-12-29T11:00:32+00:00</updated>
    <author>
            <name>Benjamin Slade</name>
            
                <email>slade@lambda-y.net</email>
            </author>
    <id>https://babbagefiles.xyz/tags/yasnippet/</id>
        
        <entry>
            <title type="html"><![CDATA[Auto-generate “creator” PDF metadata in AUCTeX using yasnippet]]></title>
            <link href="https://babbagefiles.xyz/auto-gen-hyperref-auctex-yasnippet/"/>
            <id>https://babbagefiles.xyz/auto-gen-hyperref-auctex-yasnippet/</id>
            
                    <author>
                        <name>Benjamin Slade</name>
                    </author>
            <published>2019-05-29T22:59:00-06:00</published>
            <updated>2020-04-27T13:31:00-06:00</updated>
            
            
            <content type="html"><![CDATA[<p>After struggling with some poorly-handled, apparently &ldquo;reset&rdquo; proofs
introducing heaps of errors (despite my providing a <code>.tex</code> source) for
the past few days,<sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup> I thought about providing automated pdf-tags
indicating creation tools used for my TeX-produced documents. Real,
professionally-typeset documents deserve to have the tools used to
produce them properly recognised in their metadata. So here&rsquo;s a
yasnippet which generates auto-populated <code>hyperref</code> options to
generate a <code>pdf-creator</code> tag indicating the version of Emacs, AUCTeX,
and distro used:</p>
<div class="highlight"><pre class="chroma"><code class="language-lisp" data-lang="lisp"><span class="err">#</span> <span class="nv">-*-</span> <span class="nv">mode:</span> <span class="nv">snippet</span> <span class="nv">-*-</span>
<span class="err">#</span> <span class="nv">name:</span> <span class="nv">version-hyperref</span>
<span class="err">#</span> <span class="nv">key:</span> <span class="nv">hyperv</span>
<span class="err">#</span> <span class="nv">--</span>
<span class="nv">\usepackage[pdfusetitle,</span>
 <span class="nv">pdfcreator={</span><span class="o">`</span><span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">&#34;_&#34;</span> <span class="s">&#34;-&#34;</span>
  <span class="p">(</span><span class="nv">concat</span> <span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">&#34;\n&#34;</span> <span class="s">&#34;&#34;</span> <span class="p">(</span><span class="nv">substring</span> <span class="p">(</span><span class="nv">emacs-version</span><span class="p">)</span> <span class="mi">0</span> <span class="p">(</span><span class="nf">1-</span> <span class="p">(</span><span class="nv">cl-search</span> <span class="s">&#34;(&#34;</span> <span class="p">(</span><span class="nv">emacs-version</span><span class="p">)))</span> <span class="p">))</span>
     <span class="s">&#34; with AUCTeX &#34;</span> <span class="p">(</span><span class="nv">pkg-info-version-info</span> <span class="ss">&#39;auctex</span><span class="p">)</span>
   <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;type -p lsb_release &gt; /dev/null&#34;</span><span class="p">)</span>
      <span class="p">(</span><span class="nv">concat</span>
        <span class="s">&#34; on &#34;</span>
      <span class="p">(</span><span class="nv">substring</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;lsb_release -sd&#34;</span><span class="p">)</span> <span class="mi">1</span> <span class="p">(</span><span class="nf">-</span> <span class="p">(</span><span class="nf">length</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;lsb_release -sd&#34;</span><span class="p">))</span> <span class="mi">2</span><span class="p">))</span>
        <span class="s">&#34; (&#34;</span>
      <span class="p">(</span><span class="nv">substring</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;lsb_release -sr&#34;</span><span class="p">)</span> <span class="mi">0</span> <span class="p">(</span><span class="nf">-</span> <span class="p">(</span><span class="nf">length</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;lsb_release -sr&#34;</span><span class="p">))</span> <span class="mi">1</span><span class="p">))</span>
        <span class="s">&#34; &#39;&#34;</span>
      <span class="p">(</span><span class="nv">substring</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;lsb_release -sc&#34;</span><span class="p">)</span> <span class="mi">0</span> <span class="p">(</span><span class="nf">-</span> <span class="p">(</span><span class="nf">length</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;lsb_release -sc&#34;</span><span class="p">))</span> <span class="mi">1</span><span class="p">))</span>
         <span class="s">&#34;&#39;&#34;</span> <span class="s">&#34; release, using the &#34;</span>
      <span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">&#34;\n$&#34;</span> <span class="s">&#34;&#34;</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;uname -r&#34;</span><span class="p">))</span> <span class="s">&#34; kernel)&#34;</span><span class="p">)</span>
     <span class="p">(</span><span class="k">if</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;type -p guix &gt; /dev/null&#34;</span><span class="p">)</span>
       <span class="p">(</span><span class="nv">concat</span>
         <span class="s">&#34;on Guix System &#34;</span>
       <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;guix system -V | awk &#39;NR==1{printf $5}&#39;&#34;</span><span class="p">)</span>
         <span class="s">&#34; (using the &#34;</span>
       <span class="p">(</span><span class="nv">replace-regexp-in-string</span> <span class="s">&#34;\n$&#34;</span> <span class="s">&#34;&#34;</span> <span class="p">(</span><span class="nv">shell-command-to-string</span> <span class="s">&#34;uname -r&#34;</span><span class="p">))</span> <span class="s">&#34; kernel)&#34;</span><span class="p">)))))</span><span class="o">`</span><span class="nv">}]{hyperref}</span>
</code></pre></div><p>Snippets can execute elisp code placed between <code>`...`</code>. The version of
Emacs (with some of the additional less-relevant information removed
via <code>substring</code> combined with <code>cl-search</code> (for &ldquo;(&quot;, the beginning of
the additional information)), and AUCTeX can easily be done using
Emacs-internal functions, <code>(emacs-version)</code> and
<code>(pkg-info-version-info 'auctex)</code>, respectively.</p>
<p>For the operating-system information, we rely on external calls to the
shell via Emacs&rsquo;s <code>shell-command-to-string</code> function. Many distros
will have <code>lsb_release</code> application (from the <a href="https://en.wikipedia.org/wiki/Linux%5FStandard%5FBase">Linux Standard Base
project</a>) available (and presumably usually part of the
base-install). Running <code>lsb_release --help</code> reveals a helpful set of
options:</p>
<div class="highlight"><pre class="chroma"><code class="language-shell" data-lang="shell">SYNOPSIS
      lsb_release <span class="o">[</span>OPTION<span class="o">]</span>...
OPTIONS
      −v, −−version
      Display the version of the LSB specification against which the distribution is compliant.

      −i, −−id
      Display the string id of the distributor.

      −d, −−description
      Display the single line text description of the distribution.

      −r, −−release
      Display the release number of the distribution.

      −c, −−codename
      Display the codename according to the distribution release.

      −a, −−all
      Display all of the above information.

      −s, −−short
      Display all of the above information in short output format.

      −h, −−help
      Display this message.
LSB Version:	1.0
</code></pre></div><p>And we can get various bits of information about the distribution out
via passing different flags to <code>lsb_release</code> (combined with the <code>-s</code>
&ldquo;short description&rdquo; flag).</p>
<p>Some distros don&rsquo;t use the <code>lsb_release</code>, and looking at <a href="https://github.com/dylanaraps/neofetch/blob/master/neofetch">various
methods <code>neofetch</code> employs to manage to suss out the distro</a> was
instructive. I added a method to allow for detecting the version of
<a href="https://www.gnu.org/software/guix/">Guix System</a> distribution, and it could be easily extended (in
neofetch-style) to employ other methods for additional distros. (But
the <code>lsb_release</code> method should handle the majority of distros.)</p>
<p>Finally, we get can the kernel version out via the ubiquitous <code>uname</code>
application, via <code>uname -r</code>.</p>
<p>Thus calling this snippet on <a href="https://voidlinux.org/">Void Linux</a> results in a TeX block like this:</p>
<div class="highlight"><pre class="chroma"><code class="language-latex" data-lang="latex"><span class="k">\usepackage</span>[pdfusetitle,
pdfcreator=<span class="nb">{</span>GNU Emacs 26.2 with AUCTeX 12.1.2 on Void Linux
(rolling &#39;void&#39; release, using the 5.1.5-1 kernel)<span class="nb">}</span>]<span class="nb">{</span>hyperref<span class="nb">}</span>
</code></pre></div><p>(The <code>hyperref</code> option <code>pdfusetitle</code> will make use of the values of
the TeX-defined <code>\author{...}</code> and <code>\title={...}</code> commands to populate
the author and title pdf metadata. If you do anything funky with these
fields (like adding a <code>\thanks{...}</code> command to your title), you may
need to take out the <code>pdfusetitle</code> option and manually specify these
tags via <code>pdfauthor={...}</code> etc.)</p>
<p>On <a href="https://www.gnu.org/software/guix/">Guix System</a> (née GuixSD) the result will be something like:</p>
<div class="highlight"><pre class="chroma"><code class="language-latex" data-lang="latex"><span class="k">\usepackage</span>[pdfusetitle,
pdfcreator=<span class="nb">{</span>GNU Emacs 26.2 with AUCTeX 12.1.2
on Guix System 1.0.1-1.8204295 (using the 5.1.2-gnu kernel)<span class="nb">}</span>]<span class="nb">{</span>hyperref<span class="nb">}</span>
</code></pre></div><p>An amusing side-discovery of putting this snippet together is that
<a href="https://www.archlinux.org/">Arch Linux</a> multiply – and insistently – reminds the <code>uname</code> caller of
the distro the kernel belongs to. I.e., using this snippet on my Arch
machine resulted in:</p>
<div class="highlight"><pre class="chroma"><code class="language-latex" data-lang="latex"> <span class="k">\usepackage</span>[pdfusetitle,
pdfcreator=<span class="nb">{</span>GNU Emacs 26.2 with AUCTeX 12.1.2 on
Arch Linux (rolling &#39;n/a&#39; release, using the 5.1.5-arch1-2-ARCH kernel)<span class="nb">}</span>]<span class="nb">{</span>hyperref<span class="nb">}</span>
</code></pre></div><section class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1" role="doc-endnote">
<p>The horrifying reality of &ldquo;professional&rdquo; desktop publishing software stands in contrast to real professional documents produced in (La)TeX; and any resulting professionalism in the output of the former results from squeezing <strong>additional</strong> unpaid labour from researchers who have to correct the errors of &ldquo;professional&rdquo; typesetters. <a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</section>
]]></content>
            
                 
                    
                         
                        
                            
                             
                                <category scheme="https://babbagefiles.xyz/categories/emacs" term="emacs" label="emacs" />
                             
                                <category scheme="https://babbagefiles.xyz/categories/latex" term="latex" label="latex" />
                            
                        
                    
                 
                    
                 
                    
                         
                        
                            
                             
                                <category scheme="https://babbagefiles.xyz/tags/auctex" term="auctex" label="auctex" />
                             
                                <category scheme="https://babbagefiles.xyz/tags/yasnippet" term="yasnippet" label="yasnippet" />
                            
                        
                    
                
            
        </entry>
    
</feed>
