My 404s Don't code and drive

About

Removing Hypertarget For Sections When Converting Markdown to Latex Using Pandoc

When converting Markdown to Latex using Pandoc 2 it puts hypertarget markers on each section like this,

\hypertarget{my-chapter}{
\section{My Chapter}\label{my-chapter}}

This can get annoying if you already defined a label or you have several sections named the same thing. For example, I have a document that have several input files each with the same section name and I convert them separately. This creates a lot of confusion and warnings when rendering to a pdf. Fortunatley it can be fixed by writing markdown-auto_identifiers instead of just markdown. The conversion becomes,

pandoc my_file.md -f markdown-auto_identifiers -t latex -o my_file.tex

Writing Long Lists to File in Python With Enforced Line Length

I had a long array of numbers that I wanted to write to file such that the numbers would be comma separated and with a line break at 80 chars. I tried with numpy savetxt but there is always a problem with it so I resorted to the textwrap library.

import numpy as np
import textwrap
long_arr = np.array([1, 2, 3, ...., n])
long_str = 'long_arr = [' + ', '.join(long_arr.astype(str)) + ']'
print(textwrap.fill(long_str, 80), file=open('long_arr.txt','w'))

Python Problem Using dict.fromkeys() to Create a Dictionary

I had this bug somewhere in the code and I couldn’t figure out where it was. After isolating some code I came across this behavior that I didn’t expect.

my_list = ['dic1', 'dic2']
my_dic = dict.fromkeys(mylist, {})
>>> my_dic
{'dic1': {}, 'dic2': {}}
my_dic['dic1']['entry1'] = 1
>>> my_dic
{'dic1': {'entry1': 1}, 'dic2': {'entry1': 1}}

It turns out that if the default value is a dictionary then each entry in the newly created dictionary will have a reference to that dictionary. This happens for every mutable object we put as default argument. Simply put, change one entry in the base dictionary and all nested dictionaries will update. It seems like a lot of people went into this trap judging from the numerous StackOverflow Questions

We can fix this by writing,

my_list = ['dic1', 'dic2']
new_dic = dict((key,{}) for key in my_list)
new_dic
>>> {'dic1': {}, 'dic2': {}}
new_dic['dic1']['entry1'] = 1
>>> new_dic
{'dic1': {'entry1': 1}, 'dic2': {}}

Freeing Up Space by Deleting Old Tex Versions

Do you use TeX (Latex) on your Mac or Ubuntu? It turns out that updating Latex does not delete older version files. They are all stored in /usr/local/texlive/. By deleting older versions I managed to free up almost 20GB!!! OMG.

Putting a Colored Frame Around an Image in Latex

Sometimes it is nice to put a colored frame around an image in your Latex document. The ordinary frame command does not support coloring frames but we can use to two packages to achieve xcolor and adjustbox.

\usepackage[HTML]{xcolor}
\usepackage[export]{adjustbox}

The xcolor package allows us to use the HMTL Hex encoding of colors which is very convenient. However, it seems you need to define the color name to use it in for the image, like this,

\definecolor{my_magenta}{HTML}{FF00FF}

To put a frame around an image we just add some parameters to the includegraphics command,

\includegraphics[cfbox=my_magenta 0.5pt 0pt]{my_image}

Where the parameters are (color_name, border_width, inner_padding) where the inner_padding parameter is a white colored border inside the box border. There are many more parameters to explore, e.g. background, etc.