<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: pwsmith</title>
    <description>The latest articles on DEV Community by pwsmith (@pwsmith).</description>
    <link>https://dev.to/pwsmith</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F190790%2F726df5c4-216f-47ee-95b9-eafb84dd3ef2.jpg</url>
      <title>DEV Community: pwsmith</title>
      <link>https://dev.to/pwsmith</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pwsmith"/>
    <language>en</language>
    <item>
      <title>Fully Formatted Citations in Reveal.js</title>
      <dc:creator>pwsmith</dc:creator>
      <pubDate>Sun, 01 Nov 2020 14:59:59 +0000</pubDate>
      <link>https://dev.to/pwsmith/fully-formatted-citations-in-reveal-js-3m00</link>
      <guid>https://dev.to/pwsmith/fully-formatted-citations-in-reveal-js-3m00</guid>
      <description>&lt;p&gt;In this post I will show how to make a fully formatted citation list if you have made slides in reveal.js. This is useful if you are giving an academic presentation with the slides, and want to follow the convention of giving a short citation note in the slide, with the full information at the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why?
&lt;/h2&gt;

&lt;p&gt;One of the drawbacks of reveal.js, that I noted &lt;a href="https://pwsmith.github.io/2020/01/08/reveal.js-for-linguistics/"&gt;in an overview&lt;/a&gt; is that there is no native way of making a citation list in html. That is, there is no equivalent of bibtex/biblatex that will automatically scan your file for citation keys, and output a formatted list of references at the end of your document based upon what it finds. In the same post, I outlined why I don't think that this is a problem in and of itself — a reference list is effectively a way of pointing someone viewing your slides to a source, and there are html-based ways of doing that, such as making a hyperlink using the &lt;code&gt;&amp;lt;a&amp;gt;...&amp;lt;/a&amp;gt;&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;Yet, despite some positives, there are a couple of drawbacks: unpublished work that is not associated with a url is difficult to incorporate, and, whilst it is easy to click on links if you are viewing the slides at a later point, if you are in the audience at the talk and don't have online access to the slides, then you can't check which exact source is being used.&lt;sup id="fnref1"&gt;1&lt;/sup&gt; It is also a convention of academic work that one provides references as well, so it is understandable to want a formatted list at the end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;This method requires use of the command line and the ability to run a python script. So, you should have a basic knowledge of the command line on your computer (nothing complex, but how to open the terminal, how to navigate to a directory, and how to put in basic commands), as well as having &lt;a href="https://www.python.org/"&gt;Python&lt;/a&gt; installed. Furthermore, we will be using &lt;a href="https://pandoc.org"&gt;Pandoc&lt;/a&gt; to generate the citation list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Python
&lt;/h3&gt;

&lt;p&gt;You can check if Python is already installed on your computer by typing into the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the output gives (with the value of X depending on what version is installed on your system), then you're good.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Python 3.X.X
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that if you are using a Mac, it's likely that you'll have both Python 2 and Python 3 installed.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; You can check if Python 3 is available on your system by typing:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 &lt;span class="nt"&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this is the case, then wherever I use the command &lt;code&gt;python&lt;/code&gt; in this guide, you should replace it with &lt;code&gt;python3&lt;/code&gt;. If you receive a notice &lt;code&gt;command not found: python&lt;/code&gt;, then you need to &lt;a href="https://www.python.org/downloads/"&gt;install python&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Pandoc
&lt;/h3&gt;

&lt;p&gt;Pandoc is a command line tool useful for converting documents from one file type to another. Instructions for installing it on a variety of systems can be found &lt;a href="https://pandoc.org/installing.html"&gt;here&lt;/a&gt;. We will use the filter &lt;code&gt;pandoc-citeproc&lt;/code&gt;, which sometimes is not included in the installation (e.g. if you're installing on Linux).&lt;/p&gt;

&lt;h3&gt;
  
  
  Bibliography
&lt;/h3&gt;

&lt;p&gt;Finally, you'll need a LaTeX style &lt;code&gt;.bib&lt;/code&gt; file. If you don't have one already, you can make one using &lt;a href="https://bibdesk.sourceforge.io/"&gt;Bibdesk&lt;/a&gt;, &lt;a href="https://www.jabref.org/"&gt;Jabref&lt;/a&gt; or &lt;a href="https://www.zotero.org/"&gt;Zotero&lt;/a&gt; (amongst others).&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Incorporating citation keys
&lt;/h2&gt;

&lt;p&gt;The first step is to incorporate citation keys within the document. In &lt;a href="https://pwsmith.github.io/2020/01/08/reveal.js-for-linguistics/"&gt;my overview&lt;/a&gt;, I noted that given that reveal.js makes slide decks that are web-based, it makes sense to make use of this and cite other works as a hyperlink where possible, to take the reader right to the selected work. I will take this approach here. Given that the slides are written in html, we will be using the &lt;code&gt;&amp;lt;a&amp;gt;...&amp;lt;/a&amp;gt;&lt;/code&gt; tag, where (the &lt;code&gt;href&lt;/code&gt; value is set to the webpage of the citation, and the text between the tags is what you want displayed on the slide). Below is a sample document before we've put in the citation keys, which I'll be using as a toy example for the remainder of the tutorial:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- aba.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"linguistic-examples.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Generating html references&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://www.glossa-journal.org/article/10.5334/gjgl.362/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Moskal (2018)&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; shows that suppletion for the exclusive happens only when the inclusive pronoun is also suppletive.
    This follows in work on *ABA patterns in suppletion in &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://mitpress.mit.edu/books/universals-comparative-morphology"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bobaljik (2012)&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; and &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://link.springer.com/article/10.1007%2Fs11049-018-9425-0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Smith et al (2019)&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to eventually access the bibliography entry for the citation, one should also incorporate a citation key using &lt;code&gt;data-citation-key&lt;/code&gt;, which is a custom attribute added to &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; elements in order to specify a citation key.&lt;sup id="fnref3"&gt;3&lt;/sup&gt;  The value of &lt;code&gt;data-citation-key&lt;/code&gt; should correspond to the citation key in your .bib file. As we will eventually be converting a markdown document to generate the bibliography, we need to include &lt;code&gt;@&lt;/code&gt; immediately before the citation key value, as this is the citation identifier in markdown. A basic citation would then look like the following:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://www.glossa-journal.org/article/10.5334/gjgl.362/"&lt;/span&gt; &lt;span class="na"&gt;data-citation-key=&lt;/span&gt;&lt;span class="s"&gt;"@moskal2017"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Moskal (2018)&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: the value &lt;em&gt;after&lt;/em&gt; &lt;code&gt;@&lt;/code&gt; in the field for &lt;code&gt;data-citation-key&lt;/code&gt; &lt;em&gt;must&lt;/em&gt; match the citation key in your .bib file. That is, the value of &lt;code&gt;data-citation-key&lt;/code&gt; should be of the form &lt;code&gt;@+cite_key&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our toy source file would then look like the following, once the &lt;code&gt;data-citation-key&lt;/code&gt; values are added:&lt;sup id="fnref4"&gt;4&lt;/sup&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"linguistic-examples.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;Generating html references&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://www.glossa-journal.org/article/10.5334/gjgl.362/"&lt;/span&gt; &lt;span class="na"&gt;data-citation-key=&lt;/span&gt;&lt;span class="s"&gt;"@moskal2017"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Moskal (2018)&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; shows that suppletion for the exclusive happens only when the inclusive pronoun is also suppletive.
    This follows in work on *ABA patterns in suppletion in &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://mitpress.mit.edu/books/universals-comparative-morphology"&lt;/span&gt; &lt;span class="na"&gt;data-citation-key=&lt;/span&gt;&lt;span class="s"&gt;"@bobaljik2012"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bobaljik (2012)&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt; and &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://link.springer.com/article/10.1007%2Fs11049-018-9425-0"&lt;/span&gt; &lt;span class="na"&gt;data-citation-key=&lt;/span&gt;&lt;span class="s"&gt;"@smithetal2016"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Smith et al (2019)&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Extracting the citation keys
&lt;/h2&gt;

&lt;p&gt;Once all of the citation keys are in the html source file, they need to be extracted so that there is a list containing all the citation keys that you want to then be references. We will use Python for this step. Overall, we want to be able to call the .py script from the command line, and in the same command specify which .html source file to look for the citations in. So, we will aim for the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python SCRIPT.py SOURCE.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For the first step, we will use the &lt;code&gt;sys&lt;/code&gt; library in python, which should already be present in your Python installation. &lt;br&gt;
In order to extract the citation keys we will use &lt;a href="https://www.crummy.com/software/BeautifulSoup/"&gt;Beautiful Soup&lt;/a&gt;, which is a Python library for processing the html code underlying web pages. Firstly, install Beautiful Soup on your system using the following at the command line:&lt;sup id="fnref5"&gt;5&lt;/sup&gt;&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--user&lt;/span&gt; beautifulsoup4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Make a file — we'll call it &lt;code&gt;refgrab.py&lt;/code&gt; here, but the name doesn't matter too much — and insert the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# rebgrab.py
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;sys&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;bs4&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BeautifulSoup&lt;/span&gt;

&lt;span class="nb"&gt;file&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"r"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;fp&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'data-citation-key'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this does:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Imports the &lt;code&gt;sys&lt;/code&gt; library.&lt;/li&gt;
&lt;li&gt;Imports the Beautiful Soup library.&lt;/li&gt;
&lt;li&gt;Scans the file picked out by &lt;code&gt;sys.argv[1]&lt;/code&gt; (the second file you specify on the command line, in our case, &lt;code&gt;aba.html&lt;/code&gt;), looks for all &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; elements in that file and returns a list of each value of &lt;code&gt;data-citation-key&lt;/code&gt; associated with each &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt;.&lt;sup id="fnref6"&gt;6&lt;/sup&gt; This list is what we will use to build our reference list.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We can run this script on our toy example using the following:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python refgrab.py aba.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will return the following to the terminal window:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@moskal2017
@bobaljik2012
@smithetal2016
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 3: Making the reference list
&lt;/h2&gt;

&lt;p&gt;Now that we have a way to extract all the citation keys from our html document, we need a way to allow Pandoc to access them. One way to do this is to print the output of &lt;code&gt;refgrab.py&lt;/code&gt; to its own file. To do this, redirect the output of &lt;code&gt;refgrab.py&lt;/code&gt; to a temporary markdown file &lt;code&gt;refs_temp.md&lt;/code&gt; that we'll delete later on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python refgrab.py aba.html &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; refs_temp.md
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will produce a document like the following, from our sample file above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!--- refs_temp.md ---&amp;gt;&lt;/span&gt;
@moskal2018
@bobaljik2012
@smithetal2016
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, if you don't want to generate an intermediate file, you can pipe the output of the python script directly to Pandoc, as described later on.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Generating the citation list
&lt;/h2&gt;

&lt;p&gt;With the file &lt;code&gt;refs_temp.md&lt;/code&gt;, we can now build a citation list of what is contained within. To do this, run the following command, subsituting the bibliography path for the relevant one for your bibliography. If you chose a name other than &lt;code&gt;refs_temp&lt;/code&gt; for the markdown file in the previous step, use that name instead of &lt;code&gt;refs_temp.&lt;/code&gt;&lt;sup id="fnref7"&gt;7&lt;/sup&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pandoc &lt;span class="nt"&gt;--bibliography&lt;/span&gt; /home/pwsmith/Dropbox/Ducks/biblio.bib &lt;span class="nt"&gt;--filter&lt;/span&gt; pandoc-citeproc refs_temp.md &lt;span class="nt"&gt;-o&lt;/span&gt; references.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 5: Incorporating the citations into your document
&lt;/h2&gt;

&lt;p&gt;Finally, with the references generated, you should end up with a file called &lt;code&gt;references.html&lt;/code&gt; with the following as its content:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- references.html --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"citation"&lt;/span&gt; &lt;span class="na"&gt;data-cites=&lt;/span&gt;&lt;span class="s"&gt;"moskal2017"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Moskal (2018)&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"citation"&lt;/span&gt; &lt;span class="na"&gt;data-cites=&lt;/span&gt;&lt;span class="s"&gt;"bobaljik2012"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Bobaljik (2012)&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt; &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"citation"&lt;/span&gt; &lt;span class="na"&gt;data-cites=&lt;/span&gt;&lt;span class="s"&gt;"smithetal2016"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Smith et al. (2019)&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"refs"&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"references hanging-indent"&lt;/span&gt; &lt;span class="na"&gt;role=&lt;/span&gt;&lt;span class="s"&gt;"doc-bibliography"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"ref-bobaljik2012"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Bobaljik, Jonathan D. 2012. &lt;span class="nt"&gt;&amp;lt;em&amp;gt;&lt;/span&gt;Universals in Comparative Morphology&lt;span class="nt"&gt;&amp;lt;/em&amp;gt;&lt;/span&gt;. Cambridge, MA: MIT Press.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"ref-moskal2017"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Moskal, Beata. 2018. “Exclusively excluding the Exclusive: Suppletion Patterns in Clusivity.” &lt;span class="nt"&gt;&amp;lt;em&amp;gt;&lt;/span&gt;Glossa&lt;span class="nt"&gt;&amp;lt;/em&amp;gt;&lt;/span&gt; 2018.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"ref-smithetal2016"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Smith, Peter W., Beata Moskal, Ting Xu, Jungmin Kang, and Jonathan D. Bobaljik. 2019. “Case and Number Suppletion in Pronouns.” &lt;span class="nt"&gt;&amp;lt;em&amp;gt;&lt;/span&gt;Natural Language and Linguistic Theory&lt;span class="nt"&gt;&amp;lt;/em&amp;gt;&lt;/span&gt; 37 (3): 1029–1101.&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pandoc generates both the reference list, which is what we are after, as well as the intext citations, which we don't need. What remains then is to select all the lines contained within the &lt;code&gt;&amp;lt;div id="refs"&amp;gt;&lt;/code&gt; and copy-paste that into the appropriate place in your html document. Note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you're making a webpage, then you can most likely copy the entire "refs" div.&lt;/li&gt;
&lt;li&gt;If you're making slides in reveal.js, then you'll most likely need to split the references over multiple slides so put in &lt;code&gt;&amp;lt;section&amp;gt;...&amp;lt;/section&amp;gt;&lt;/code&gt; elements where needed (and don't copy the entire "refs" div).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 6: Deleting the temp files (optional)
&lt;/h2&gt;

&lt;p&gt;The last step is to remove the file &lt;code&gt;refgrab.py&lt;/code&gt;, and, if you want, the &lt;code&gt;references.html&lt;/code&gt;, as they are no longer needed and can be easily regenerated following the steps above if they are. To remove them, simply run the command:&lt;sup id="fnref8"&gt;8&lt;/sup&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;rm &lt;/span&gt;refs_temp.md references.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Saving time
&lt;/h2&gt;

&lt;p&gt;If you're comfortable with all of the above steps, you can simply run one command (and not build &lt;code&gt;refs_temp.md&lt;/code&gt;) by piping the output of the python script directly into pandoc. In which case, steps 3 and 4 are conflated into one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python refgrab.py SOURCE.html | pandoc &lt;span class="nt"&gt;--bibliography&lt;/span&gt; /home/pwsmith/Dropbox/Ducks/biblio.bib &lt;span class="nt"&gt;--filter&lt;/span&gt; pandoc-citeproc &lt;span class="nt"&gt;-o&lt;/span&gt; references.html
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, be sure to put in the relevant names and pathways for the python script, source file and bibliography.&lt;/p&gt;

&lt;h2&gt;
  
  
  Notes
&lt;/h2&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;That said, if you use reveal.js to make slides, then it makes sense to host the slides online, say for instance on Github Pages, and give the audience a link at the beginning of the talk. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;This may also be the case for Windows, but I don't know. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;The only thing that is important here is the prefix &lt;code&gt;data&lt;/code&gt;; &lt;code&gt;-citation-key&lt;/code&gt; is used as its semantically easy to follow. However, one can use whatever you want, e.g. &lt;code&gt;data-key&lt;/code&gt;, &lt;code&gt;data-citation&lt;/code&gt;, &lt;code&gt;data-ref&lt;/code&gt;. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn4"&gt;
&lt;p&gt;In my bibliography, the years of the first and third citation key doesn't match the publication year. This doesn't matter, I'm just noting in case you're wondering &lt;em&gt;why&lt;/em&gt; it appears that that is the case. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn5"&gt;
&lt;p&gt;If you are using a Mac with both Python 2 and Python 3 installed, you should use pip3 instead of pip. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn6"&gt;
&lt;p&gt;If you use &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; elements that are not citations, then you will end up with some empty references in the markdown file that is generated. Dont' worry about this: Pandoc will run fine anyway. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn7"&gt;
&lt;p&gt;You can pick whatever names you want for this file, and &lt;code&gt;references.html&lt;/code&gt;: they are temporary ones and we'll delete them in Step 6. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn8"&gt;
&lt;p&gt;Agian, if you used different names for the intermediate files, be sure to use those instead. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>html</category>
      <category>python</category>
      <category>slides</category>
      <category>revealjs</category>
    </item>
    <item>
      <title>A bash function to make a book index</title>
      <dc:creator>pwsmith</dc:creator>
      <pubDate>Fri, 30 Oct 2020 15:47:47 +0000</pubDate>
      <link>https://dev.to/pwsmith/a-bash-function-to-make-a-book-index-30f3</link>
      <guid>https://dev.to/pwsmith/a-bash-function-to-make-a-book-index-30f3</guid>
      <description>&lt;h1&gt;
  
  
  Scope of the issue
&lt;/h1&gt;

&lt;p&gt;Commonly, in non-fiction there is an index at the end to allow readers to look at a collated list of topics that are contained within with page numbers where they are discussed, so the reader can go directly there without having to scan through the whole book each time. Books written in LaTeX can make use of the &lt;code&gt;imakeidx&lt;/code&gt; package to generate the index. The package is &lt;a href="https://www.ctan.org/pkg/imakeidx"&gt;well documented&lt;/a&gt;, and I won't discuss it in any real detail here, as there are tutorials online for how to use it.&lt;sup id="fnref1"&gt;1&lt;/sup&gt; What's important is that each term that you want to index is followed by the command &lt;code&gt;\index{TERM}&lt;/code&gt;. So, say that you want to index &lt;code&gt;second&lt;/code&gt; in the text below, you'd add in &lt;code&gt;\index{second}&lt;/code&gt; like so:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tex"&gt;&lt;code&gt;A second&lt;span class="k"&gt;\index&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;second&lt;span class="p"&gt;}&lt;/span&gt; is one of the fundmental units of time.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In an ideal world, you would create the index as you write, so that each time you put in a term, you already put the index marker in the source text. However, writing a book doesn't really work that way and, &lt;a href="https://www.degruyter.com/view/title/542047"&gt;take it from me&lt;/a&gt;, what you end up writing will rarely match exactly what you set out to write. It's better then to do the index at the end of the project, when you can see the book in context and know the contents.&lt;/p&gt;

&lt;p&gt;The problem is, then you have to go through your source files and put the &lt;code&gt;\index{TERM}&lt;/code&gt; command after everything you wish to index. That's a daunting prospect for a long book. So, what is the best way to do this?&lt;/p&gt;

&lt;h1&gt;
  
  
  The obvious
&lt;/h1&gt;

&lt;p&gt;Suppose you have gone through your work, noted which are the important terms you want in the index, conceptualised how they all group together (i.e. what stands alone, what categories can be nested under others etc.) and you're ready to do it. The obvious thing to do is to go to the beginning of the pdf, start reading, and wherever you see something you want to index, run synctex, find the item in the source, and input the relevant index marker. Easy, right? This is certainly one way to do it, and not an inherently bad one, but there are some downsides. It is repetitive, in many many cases redundant, as you will enter the same text for the same item in various different places, and most likely, you are going to miss some. Why? Well, if you have 50 items you want to index, you need to keep the entire group of 50 in your mind whilst you read through. You &lt;em&gt;may not&lt;/em&gt; miss some, but chances are you will. So, whilst this is an option, it seems there is a better way. Let's call this Plan B for now.&lt;/p&gt;

&lt;h1&gt;
  
  
  Replace All with a GUI?
&lt;/h1&gt;

&lt;p&gt;As I noted before, indexing in LaTeX is pretty easy.&lt;br&gt;
Suppose that you want to index &lt;em&gt;second&lt;/em&gt;, then all you need to do is add &lt;code&gt;\index{second}&lt;/code&gt; after every instance of &lt;em&gt;second&lt;/em&gt;. Simple. It effectively boils down to &lt;code&gt;second&lt;/code&gt; -&amp;gt; &lt;code&gt;second\index{second}&lt;/code&gt;. There's two ways of looking at this. &lt;br&gt;
Firstly, one can &lt;em&gt;append&lt;/em&gt; &lt;code&gt;\index{second}&lt;/code&gt; to every instance of &lt;code&gt;second&lt;/code&gt;. Or you can &lt;em&gt;replace&lt;/em&gt; the string &lt;code&gt;second&lt;/code&gt; with &lt;code&gt;second\index{second}&lt;/code&gt;. The effect is the same, in the sense that the result is one of appending the index command. However, the subtle difference is that you replace the original string, but the replacement string contains the original string.&lt;/p&gt;

&lt;p&gt;The next obvious solution is that most text editors have a function that allows you to find and replace text.Usually by hitting something like &lt;code&gt;control/command+f&lt;/code&gt; you can get the editor to scan through the document and find the next instance of the string you're searching for. Then, depending on your editor, there is often an option to &lt;em&gt;replace&lt;/em&gt; that with a different one. We can then make a simpler version of the procedure above, and for a specific term, programmatically go through the document hitting &lt;code&gt;find&lt;/code&gt; and then &lt;code&gt;replace&lt;/code&gt;. Again however, there is a problem. This is easier than earlier, but still redundant in that you are repeating the same step over and over.&lt;/p&gt;

&lt;p&gt;So, how about the &lt;code&gt;replace all&lt;/code&gt; option, that will at a click replace &lt;em&gt;every&lt;/em&gt; instance of the chosen string with the alternative? That's what we're striving for, right? The procedure now is simple, hit &lt;code&gt;control+f&lt;/code&gt;, enter &lt;em&gt;second&lt;/em&gt; in the then click &lt;code&gt;replace all&lt;/code&gt;. Easy. Repeat that 50 times for your terms, and you have yourself an index. Right? Well, yes, but probably not the one you want.&lt;/p&gt;

&lt;p&gt;Two problems. Firstly, language is, frankly, annoying for this task.&lt;sup id="fnref2"&gt;2&lt;/sup&gt; Homophony, where the same string means different things, means that not every instance of the word &lt;em&gt;second&lt;/em&gt; is going to be right. If you want to index the unit of time, then with &lt;code&gt;replace all&lt;/code&gt; you may also end up indexing &lt;em&gt;second&lt;/em&gt; in the sense of "second place", which is not ideal. You may even use the verb &lt;em&gt;seconded&lt;/em&gt;, which is going to get conflated with the unit of time. Again, not ideal. The second (get it?) issue is that books are long, and best practice with LaTeX is to split long documents up into smaller parts and call them from a main document with the &lt;code&gt;\include{}&lt;/code&gt; or &lt;code&gt;\input{}&lt;/code&gt; command. You don't have to write this way, but if you compile a 300 page document each time, it's going to take a while and this can be annoying when debugging. &lt;code&gt;Replace all&lt;/code&gt; often only works on a single document, and not for all documents in an entire directory. So, if you have 6 chapters of a book, all split into different files, then you have to run the same command six times. We're starting to zero in on the solution, but we need a better way still, that allows us to replace all in one go, but important (i) quickly check what we're replacing; and (ii) do all of our source files at once.&lt;/p&gt;
&lt;h1&gt;
  
  
  Enter bash: find and replace with &lt;code&gt;sed&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;Doing the same task on a batch of files in one go is ideally suited to using the shell of your computer. There are a number of different shells available, with &lt;em&gt;bash&lt;/em&gt; and &lt;em&gt;zsh&lt;/em&gt; being the most common on unix based systems (Linux, Mac and BSD, for instance) and &lt;em&gt;powershell&lt;/em&gt; on Windows.  I'll use bash in what follows. Find-and-replace can be done on the command line using &lt;em&gt;sed&lt;/em&gt;, or 'stream editor'. It is a very powerful tool, that can also be used to append, as we'll see later. Changes can be written either to the standard output, in which case you'll see them on your terminal screen, or written into the source document, or written to a new document. We'll use the former two.&lt;/p&gt;

&lt;p&gt;Let's see a simple example. In our directory with the source files, suppse there is a file &lt;code&gt;chapter_1.tex&lt;/code&gt; that contains the following text.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tex"&gt;&lt;code&gt;A &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;second&lt;span class="p"&gt;}&lt;/span&gt; is one of the fundamental units of time.
There are 60 seconds for each &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;minute&lt;span class="p"&gt;}&lt;/span&gt;.
Beneath the level of the second is the &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;millisecond&lt;span class="p"&gt;}&lt;/span&gt;, the &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;nanosecond&lt;span class="p"&gt;}&lt;/span&gt; and the &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;microsecond&lt;span class="p"&gt;}&lt;/span&gt;.            
Second, the term, was borrowed into English from Old French &lt;span class="k"&gt;\cite&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;wiktionary&lt;span class="p"&gt;}&lt;/span&gt;.
Second to none, the second is the easiest unit to count with.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we want to use &lt;code&gt;sed&lt;/code&gt; to replace all instances of &lt;em&gt;second&lt;/em&gt;, with, say, &lt;em&gt;hour&lt;/em&gt;, then we run the following. The &lt;code&gt;s&lt;/code&gt; prefix indicates the substitution operation, and &lt;code&gt;g&lt;/code&gt; suffix indicates that the change should happen globally throughout the file, rather than just the first instance. Between the slashes, the first string is the original, and the second is the replacement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/second/hour/g'&lt;/span&gt; chapter_1.tex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this prints the following to the terminal screen:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mR6wwsh_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3bwrctcpa6bkejfyoq1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mR6wwsh_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3bwrctcpa6bkejfyoq1n.png" alt="screenshot showing second replaced by hour in standard output"&gt;&lt;/a&gt;&lt;/p&gt;
Find and replace with `sed`.



&lt;p&gt;Let's say we run that to check and we're happy. Now we want to make the change in the file, then we replace with &lt;code&gt;-e&lt;/code&gt; flag with &lt;code&gt;-i&lt;/code&gt; (in place).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/second/hour/g'&lt;/span&gt; chapter_1.tex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Compare the result of running &lt;code&gt;cat&lt;/code&gt; on the &lt;code&gt;chapter_1.tex&lt;/code&gt; before and after the command is:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lH5arhuV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9qmirwbs9l82mwwu80hu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lH5arhuV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9qmirwbs9l82mwwu80hu.png" alt="screenshot showing second replaced by hour in the file"&gt;&lt;/a&gt;&lt;/p&gt;
Note the differing outputs of running `cat chapter_1.tex` before and after `sed`.



&lt;p&gt;But suppose you also want to change the last instance also, on the last line.  This begins with a capital, so it wasn't picked up by &lt;code&gt;sed&lt;/code&gt; the first time. Fortunately, &lt;code&gt;sed&lt;/code&gt; can be combined with regular expressions, so you can make the query &lt;code&gt;[sS]econd&lt;/code&gt;, and the search will pick up both &lt;code&gt;second&lt;/code&gt; and &lt;code&gt;Second&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--V6H0F4nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cv5eqnxyr5opu4p4j3my.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--V6H0F4nh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cv5eqnxyr5opu4p4j3my.png" alt="screenshot showing second and Second replaced by hour in the file"&gt;&lt;/a&gt;&lt;/p&gt;
Using a basic regular expression with `sed`.



&lt;h1&gt;
  
  
  Making a bash function for indexing
&lt;/h1&gt;

&lt;p&gt;Now we have the basics in place of what we need to define a function that we will use for indexing. Firstly, make a file to store the function. The name doesn't matter, so we'll just call it &lt;code&gt;index.sh&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## to be revised...&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;index&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'/g'&lt;/span&gt; &lt;span class="nv"&gt;$3&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then type &lt;code&gt;source index.sh&lt;/code&gt; and the function will be added to your environment. We'll revise this function throughout.  &lt;strong&gt;Any time you make a change to the &lt;code&gt;index.sh&lt;/code&gt; file, you need to run &lt;code&gt;source index.sh&lt;/code&gt; on the command line to reimport the function into your environment.&lt;/strong&gt; This is used as follows. Typing &lt;code&gt;index&lt;/code&gt; at your terminal calls the function, then the string you wish to replace (the &lt;em&gt;query&lt;/em&gt;), the string you wish to be the replacement, and then the file you want to do it for. Bash will match the first word after &lt;code&gt;index&lt;/code&gt; to &lt;code&gt;$1&lt;/code&gt;, the second to &lt;code&gt;$2&lt;/code&gt; and the third to &lt;code&gt;$3&lt;/code&gt;. Going back to what we've been doing so far, suppose again that we wish to replace &lt;code&gt;second&lt;/code&gt; with &lt;code&gt;hour&lt;/code&gt; in &lt;code&gt;chapter_1.tex&lt;/code&gt;, at the command line run the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--K1GStHJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p6jj1mg5e8anj8enf84u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--K1GStHJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p6jj1mg5e8anj8enf84u.png" alt="first attempt at the index function"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice!&lt;/p&gt;

&lt;p&gt;But, we need some refinements. All we've done so far is replicate the GUI replace all function. But, we wanted to make it do two more things. We wanted it to allow us to check, to make sure it's all good, so we don't blindly replace everything. We'll soon add this functionality to allow us to note wrong instances, and go back and correct them later. We also wanted to be able to do it for all files in the directory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Looping over files
&lt;/h2&gt;

&lt;p&gt;Let's deal with the latter first. This, we achieve with a simple bash for-loop. &lt;strong&gt;After making this change in &lt;code&gt;index.sh&lt;/code&gt;, don't forget to run &lt;code&gt;source index.sh&lt;/code&gt; in your terminal!&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## to be revised..&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;index&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
    &lt;span class="k"&gt;for &lt;/span&gt;FILE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.tex      
    &lt;span class="k"&gt;do                               
    &lt;/span&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'/g'&lt;/span&gt; &lt;span class="nv"&gt;$FILE&lt;/span&gt;     
    &lt;span class="k"&gt;done&lt;/span&gt;                                          
&lt;span class="o"&gt;}&lt;/span&gt;    
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now no longer need to specify a file to run &lt;code&gt;sed&lt;/code&gt; on, as the bash loop will run it on all files that have the &lt;code&gt;.tex&lt;/code&gt; suffix. Let's suppose that in addition to &lt;code&gt;chapter_1.tex&lt;/code&gt; we have a second chapter, &lt;code&gt;chapter_2.tex&lt;/code&gt;, with the following contents:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tex"&gt;&lt;code&gt;A &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;second&lt;span class="p"&gt;}&lt;/span&gt; after he left, the phone rang.
He would have only had time to talk for a &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;minute&lt;span class="p"&gt;}&lt;/span&gt; anyway.
But obviously he could talk for more than a &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;millisecond&lt;span class="p"&gt;}&lt;/span&gt;, a &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;nanosecond&lt;span class="p"&gt;}&lt;/span&gt; and a &lt;span class="k"&gt;\textbf&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;microsecond&lt;span class="p"&gt;}&lt;/span&gt;.            
Second to none, the second he left was the biggest regret of his life.  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we run our modified function, we get the following to standard output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vMoSLw47--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/773vmn6aymz0th5wpqgb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vMoSLw47--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/773vmn6aymz0th5wpqgb.png" alt="running our preliminary index function over two chapters. The word 'second' in both files has been replaced by 'hour' in the standard output"&gt;&lt;/a&gt;&lt;/p&gt;
Now we're looping.



&lt;h2&gt;
  
  
  Making an actual index function
&lt;/h2&gt;

&lt;p&gt;Before moving on though, let's revise the function to make a proper index. I've been using replacement to demonstrate the use of &lt;code&gt;sed&lt;/code&gt;, but recall that we don't want to replace, but rather append &lt;code&gt;\index{TERM}&lt;/code&gt;. It's tempting to simply add in &lt;code&gt;$1&lt;/code&gt; in the replacement slot:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## to be revised..&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;index&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
    &lt;span class="k"&gt;for &lt;/span&gt;FILE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.tex    
    &lt;span class="k"&gt;do    
    &lt;/span&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'/'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\\index\{'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\}/g'&lt;/span&gt; &lt;span class="nv"&gt;$FILE&lt;/span&gt;    
    &lt;span class="k"&gt;done&lt;/span&gt;                                  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, the query will not always match the replacement, especially if the query involves a regular expression:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IwY5coRy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9ymi4wu1n3ih11ch1pc7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IwY5coRy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9ymi4wu1n3ih11ch1pc7.png" alt="using the query as part of the replacement fails with regular expressions"&gt;&lt;/a&gt;&lt;/p&gt;
This is clearly not what we want.



&lt;p&gt;The solution is to use a group in the replacement string. The escaped parentheses in the query creates a group, which is then referred to with &lt;code&gt;\1&lt;/code&gt; in the replacement string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## to be revised..&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;index&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
    &lt;span class="k"&gt;for &lt;/span&gt;FILE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.tex    
    &lt;span class="k"&gt;do    
    &lt;/span&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/\('&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\)/\1\\index\{'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\}/g'&lt;/span&gt; &lt;span class="nv"&gt;$FILE&lt;/span&gt;        
    &lt;span class="k"&gt;done&lt;/span&gt;                                  
&lt;span class="o"&gt;}&lt;/span&gt;     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zsnu5keD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/11r4p83re19hdlfrnk32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zsnu5keD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/11r4p83re19hdlfrnk32.png" alt="using the query as part of the replacement fails with regular expressions gets the right result with group variable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is better. However, it's still not quite right as we're now indexing a couple of things we shouldn't. Firstly, &lt;em&gt;nanosecond&lt;/em&gt;, &lt;em&gt;millisecond&lt;/em&gt; and &lt;em&gt;microsecond&lt;/em&gt; shouldn't get indexed. Though they contain the string &lt;em&gt;second&lt;/em&gt;, they refer to different things. Secondly, &lt;em&gt;Second to none&lt;/em&gt; has an indexed item in it, but it doesn't refer to a unit of time here, but second as in 'second place'. We can fix the former by refining our query to add in a word boundary at the beginning of the string with &lt;code&gt;\b&lt;/code&gt;. Because there is no word boundary before &lt;code&gt;second&lt;/code&gt; in, eg., &lt;code&gt;microsecond&lt;/code&gt;, then it doesn't match the query:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QM0RWbAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nuu1iywmq8gtit0zl4hp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QM0RWbAg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nuu1iywmq8gtit0zl4hp.png" alt="adding in a word boundary in the query means we don't index microsecond, for instance"&gt;&lt;/a&gt;&lt;/p&gt;
We're no longer indexing, eg., 'microsecond' with 'second'.



&lt;p&gt;For the latter case, there is not much we can do. The query matches, as it should do. The problem is, as noted earlier, there are two homophonous uses of 'second'. These tend to be fairly rare, so it's best to make a note of where they happen, and fix them by hand later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding in checking
&lt;/h2&gt;

&lt;p&gt;Now we can loop over files, let's move to the other functionality we want to add, the ability to check before replacing. Up to now we've been printing the output of &lt;code&gt;sed&lt;/code&gt; to standard output with the &lt;code&gt;-e&lt;/code&gt; flag. This provides a convenient way to check. But, we've also just been looking at the entire output. In our toy example, with two files of around five lines each, it's not a problem to look through and manually check all instanes of the query but this gets unwieldy really fast. So, we'll first run the query through &lt;code&gt;grep&lt;/code&gt;, add in colour highlighting of the query with &lt;code&gt;--color=always&lt;/code&gt;, and also use the &lt;code&gt;-H&lt;/code&gt; and &lt;code&gt;-n&lt;/code&gt; flags to show file name and line numbers respectively. This will allow us to quickly scan all and only the instances of the query, and make a note if there are any we will need to fix manually later (such as with the two instances of &lt;em&gt;Second&lt;/em&gt;, which don't fit the 'unit of time' meaning).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## to be revised..&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;index&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;    
    &lt;span class="k"&gt;for &lt;/span&gt;FILE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.tex    
    &lt;span class="k"&gt;do    
    &lt;/span&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-nH&lt;/span&gt; &lt;span class="nt"&gt;--color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;always &lt;span class="nv"&gt;$1&lt;/span&gt; &lt;span class="nv"&gt;$FILE&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/\('&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\)/\1\\index\{'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\}/g'&lt;/span&gt;        
&lt;span class="k"&gt;done&lt;/span&gt;                                           
&lt;span class="o"&gt;}&lt;/span&gt;     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A4eGRwjA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y0alarvummkp0eurvk6m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A4eGRwjA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/y0alarvummkp0eurvk6m.png" alt="output showing first filtering the query through grep, with file and line numbers"&gt;&lt;/a&gt;&lt;/p&gt;
Note the use of the single quotes in the query when we run the command. That's important for `grep` to correctly identify `\b`.



&lt;p&gt;Up until now, we've been running &lt;code&gt;sed&lt;/code&gt; with the &lt;code&gt;-e&lt;/code&gt; flag and previewing the results in standard output on the terminal, which serves as a useful check, but we also want to make the results in the file. So, as a final step, we'll add in a second run of &lt;code&gt;sed&lt;/code&gt;, this time with the &lt;code&gt;-i&lt;/code&gt; flag if the preview looks good. What we need for this is to add a if-loop into our bash function after the run of &lt;code&gt;sed&lt;/code&gt;. If we're happy with the result then we continue to run &lt;code&gt;sed -i&lt;/code&gt;, if not, the function should abort to give us a chance to refine our query. We'll also map the command line input to variables to allow them to be used throughout the function, including within the if-loop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;## Final version!&lt;/span&gt;
&lt;span class="k"&gt;function &lt;/span&gt;index&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;ARG1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;
    &lt;span class="nv"&gt;ARG2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$2&lt;/span&gt;
    &lt;span class="k"&gt;for &lt;/span&gt;FILE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.tex
    &lt;span class="k"&gt;do
    &lt;/span&gt;&lt;span class="nb"&gt;grep&lt;/span&gt; &lt;span class="nt"&gt;-nH&lt;/span&gt; &lt;span class="nt"&gt;--color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;always &lt;span class="nv"&gt;$ARG1&lt;/span&gt; &lt;span class="nv"&gt;$FILE&lt;/span&gt; | &lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s1"&gt;'s/\('&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ARG1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\)/\1\\index\{'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ARG2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\}/g'&lt;/span&gt;
    &lt;span class="k"&gt;done
&lt;/span&gt;&lt;span class="nb"&gt;read&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; &lt;span class="s1"&gt;'Is this correct (y/n): '&lt;/span&gt; VALIDATION
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;VALIDATION&lt;/span&gt;::1&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; Y &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="k"&gt;${&lt;/span&gt;&lt;span class="nv"&gt;VALIDATION&lt;/span&gt;::1&lt;span class="k"&gt;}&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; y &lt;span class="o"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;then
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Applying changes to file."&lt;/span&gt;
        &lt;span class="k"&gt;for &lt;/span&gt;SOURCE &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="k"&gt;*&lt;/span&gt;.tex
            &lt;span class="k"&gt;do
            &lt;/span&gt;&lt;span class="nb"&gt;sed&lt;/span&gt; &lt;span class="nt"&gt;-i&lt;/span&gt; &lt;span class="s1"&gt;'s/\('&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ARG1&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\)/\1\\index\{'&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$ARG2&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s1"&gt;'\}/g'&lt;/span&gt; &lt;span class="nv"&gt;$SOURCE&lt;/span&gt;
            &lt;span class="k"&gt;done
    else
        &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Function aborted, go and refine the query."&lt;/span&gt;
    &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it! Now when we want to index a term, like &lt;code&gt;second&lt;/code&gt;. All we need to do is run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;index &lt;span class="s1"&gt;'\b[sS]econd'&lt;/span&gt; second
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First &lt;code&gt;grep&lt;/code&gt; will find the relevant instances, feed that into &lt;code&gt;sed&lt;/code&gt;, which will then print to the terminal the suggested changes. A prompt will appear asking if that is correct, and if you then press &lt;code&gt;y&lt;/code&gt; or &lt;code&gt;Y&lt;/code&gt; (or type yes or Yes), then the command will run again and &lt;code&gt;sed&lt;/code&gt; will this time make the changes in the file. If not, it will abort and prompt you to refine your query.&lt;br&gt;
The result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sQ9BwOea--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yrcivg7fz3d4ntxzmily.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sQ9BwOea--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yrcivg7fz3d4ntxzmily.png" alt="Running the final function and making the changes in file"&gt;&lt;/a&gt;&lt;/p&gt;
Running the full function. Note again the second `cat` output, where the changes have been made in-file.



&lt;p&gt;It's important to note that this will &lt;em&gt;not&lt;/em&gt; do everything for us. As I said before, language doesn't allow for a perfect 1:1 mapping between terms and meanings. In our example files, some instances of &lt;code&gt;Second&lt;/code&gt; have been picked up which should not be indexed, as in &lt;em&gt;Second to none&lt;/em&gt;. However, if you refine the query enough, and carefully check before you implement the changes, you can write down the files and locations of the 'wrongly indexed' instances and go and manually change them.  This should be easy, given that the &lt;code&gt;grep&lt;/code&gt; query, with the &lt;code&gt;-nH&lt;/code&gt; flag will tell you which file, and line number the example came from. Because of the first step of the function, &lt;code&gt;grep&lt;/code&gt; tells us that we need to go and manually change line 5 in &lt;code&gt;chapter_1.tex&lt;/code&gt; and line 4 in &lt;code&gt;chapter_2.tex&lt;/code&gt;. And that's that! Our bash function saves us going through the entire file by hand making the same changes time and time again. We still need to do some manual editing, but if you take the time to craft the right regexes and combine them with this function, you'll save yourself a lot of time and energy!&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://www.overleaf.com/learn/latex/Indices"&gt;Here&lt;/a&gt; and &lt;a href="https://en.wikibooks.org/wiki/LaTeX/Indexing#imakeidx"&gt;here&lt;/a&gt;, for instance. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;Take it from me, I have a PhD in linguistics and everything. ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>latex</category>
      <category>bash</category>
      <category>writing</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
