Monthly Archives: May 2019

⁂ Asterisms in LaTeX ⁂

This is a snippet of LaTeX I put together so that I could use asterisms (⁂) when writing papers. I use them to mark off sections of text which will need further attention when editing.

unicode index:U+2042 (8258)
HTML escapes:⁂
⁂
UTF8:e2 81 82

I should really get around to cleaning up and posting the LaTeX macro files that I've been assembling over the years. And who knows, maybe there's some other STEM folks who get as excited over obscure typographical marks as I do. (There are dozens of us! Dozens!)

There are other macros floating around out there that will create asterisms, but the ones I tried don't work if you're not using single-spacing/standard leading. This one will do so — best I can tell — in addition to working with different sized text, etc.


Updated:

I've got a much, much simpler solution than the one I gave below, and it appears to get rid of the weird beginning-of-paragraph bug I sometimes ran in to with the solution I posted previously. I haven't tested it extensively, but it seems to work far better than the older version, and it's certainly much easier to understand.

\newcommand{\asterism}{%
\makebox[1em][c]{%
\makebox[0pt][c]{\raisebox{-0.8ex}{\smash{**}}}%
\makebox[0pt][c]{\raisebox{0.2ex}{\smash{*}}}%
}}

For the record, here's the old version:

\newcommand{\asterism}{%
  \smash{%
    \begin{minipage}[t]{1.2em}%
      \centering%
      \begin{spacing}{1.0}%
        \raisebox{-.15em}{%
          \setlength{\tabcolsep}{.025em}%
          \renewcommand*{\arraystretch}{0.5}%
          \resizebox{1.05em}{!}{%
            \begin{tabular}{@{}cc@{}}%
              \multicolumn{2}{c}*\$$!-0.5em]%
              *&*%
            \end{tabular}%
          }% end resizebox
        }% end raisebox
      \end{spacing}%
    \end{minipage}%
  }% end smash
}
Posted in CS / Science / Tech / Coding | Tagged , , , | Leave a comment

gifcrop.sh

Recently I had to crop a lot of animated gifs down for a project. This isn't hard to do with ImageMagick

$ convert in.gif -coalesce -repage 0x0 -crop WxH+X+y +repage out.gif

…but it does require some repetitive typing and mental arithmetic and rather mysterious incantations if you don't grok what all the coalescing and repaging is about. (I don't.) So I put together this bash script to handle that for me. Didn't Paul Graham say that if your coding is repetitive then the problem isn't your project, it's that you're doing it wrong? Specifically you're operating at the wrong level of abstraction.

Because I found myself repetitively wrangling these ImageMagick commands into shape, I decided it was time to sidestep that problem and make a script to do the drudgery for me. Plus this way I can get things like the aspect ratio before and after the changes computed for me.

#!/bin/bash

if [ -z "$1" ]; then 
  echo "usage: gifcrop.sh infile.gif left right [top] [bottom] [suffix]"
  exit
fi

echo -e "  opening \t ${1}" 1>&2

# There are several ways to extract the name of the file that comes before
# '.gif' so we can rename it with a suffix:
# BASE=${1%.gif}
BASE=$(basename ${1} .gif)
#BASE=$(echo ${1} | sed 's/.gif/\1/')

# Set the margins to be cropped off (Left, Right, Top, Bottom):
L=$2
R=$3
T=${4:-0} #use argv[4] (or 0, if argv[4] is undef or empty)
B=${5:-0} #use argv[5] (or 0, if argv[5] is undef or empty)

SUFFIX=${6:-crop} #use argv[6] (or "crop", if argv[6] is undef or empty)

# Get the original size of the image
W0=$(identify ${1} | head -1 | awk '{print $3}' | cut -d 'x' -f 1)
H0=$(identify ${1} | head -1 | awk '{print $3}' | cut -d 'x' -f 2)

aspectOld=$(printf "%4.3f" $(echo $W0/$H0 | bc -l))
echo -e "  current size \t ${W0}x${H0}\t($aspectOld)" 1>&2

# Calculate the new size of the image
let "W1 = $W0 - ($L + $R)"
let "H1 = $H0 - ($T + $B)"
aspectNew=$(printf "%4.3f" $(echo $W1/$H1 | bc -l))
echo -e "  new size \t ${W1}x${H1}\t($aspectNew)" 1>&2

NEWNAME=${BASE}${SUFFIX}.gif
echo -e "  saving to \t ${NEWNAME}" 1>&2

convert ${1} -coalesce -repage 0x0 -crop ${W1}x${H1}+${L}+${T} +repage ${NEWNAME}

Simply save this as something like gifcrop.sh, and then run it like so:

$ gifcrop.sh coolpic.gif 10 20 30 40 _small

That will take 10 pixels off the left, 20 off the right, 30 from the top and 40 from the bottom. The result gets saved as coolpic_small.gif. The new version is saved as a second file with a suffixed name instead of over-writing because I found that I had to iterate many times to get the correct dimensions, so I wanted both new and original versions available for comparison.

The final three arguments are optional, since most of the time I found myself adjusting the width but leaving the height alone, and I never encountered a situation in which I needed an alternative suffix besides "crop". So these two commands are identical:

$ gifcrop.sh in.gif 10 20 0 0 crop
$ gifcrop.sh in.gif 10 20

This all depends on the format of the results that ImageMagick gives you from the identify command, which is used to get the current size of the input image. You may need to adjust these two lines:

W0=$(identify ${1} | head -1 | awk '{print $3}' | cut -d 'x' -f 1)
H0=$(identify ${1} | head -1 | awk '{print $3}' | cut -d 'x' -f 2)

On my machine, identify foo.gif | head -1 gives me this output:

foo.gif[0] GIF 250x286 250x286+0+0 8-bit sRGB 128c 492KB 0.000u 0:00.030

The awk command isolates the 250x286 part, and the cut command pulls out the two dimensions from that.

I should probably put in --quiet or --verbose options to suppress the output, but honestly I like seeing it as an error/sanity check and adding more optional args would put me in a place where I should really re-write this to take in key/value pairs instead of positionals. As an alternative to a --quiet option, you can just pipe the output of stderr to /dev/null to make it go away if it really bothers you, e.g.:

gifcrop.sh foo.gif 100 100 2> /dev/null

I suppose I can't have a blog post about animated gifs without including at least one animated gif. So here's the most recent use I've had for gifcrop.sh, editing a gif of the "Datasaurus Dozen" for use in the Data Science class I'm teaching.

Datasaurus Dozen
Original gif of twelve very different datasets, all with equivalent summary statistics.
Datasaurus Dozen, cropped.
The same scatter plots, but with the statistics cropped out so as not to ruin the punchline in class.
Posted in CS / Science / Tech / Coding | Tagged | Leave a comment