Checking TeX files for cite-errors

Today I had to TeX a longer file with numerous \cite[somestuff]{citekey} sequences, of which some had hard-to-spot errors (some ‘]’ were missing). The following Python script checks if each \cite command has matching '[]' and '{}' sequences. I’ll probably extend this script to accept simple \cite{citekey} sequences that have no optional parameter.

#!/usr/bin/python
import re
 
with open('filetocheck.tex') as f:
	content = f.readlines()
 
index = 0
 
for a in content:
	index = index + 1
	if "cite"  in a: # check only lines that contain 'cite'
		matches = re.search(r'(.*)\[(.*)\]\{(.*)\}',a) # search for <sometext>[<sometext>]{<sometext>}
		if not matches:
			print (">>> Fail in row" , str(index), a)

Beamer Slides Template using Source Sans Fonts

Here’s a short example for a Beamer slide template using the Adobe Source Sans fonts.

\documentclass[12pt,ngerman]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{booktabs}
\usepackage{babel}
\usepackage{graphicx}
\usepackage{csquotes}
\usepackage{paralist}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{sourcesanspro}
\usepackage{sourcecodepro}
 
\definecolor{colBack}{rgb}{1,1,0.8}
\definecolor{colKeys}{rgb}{0,0,1}
\definecolor{colIdentifier}{rgb}{0,0,0}
\definecolor{colComments}{rgb}{1,0,0}
\definecolor{colString}{rgb}{0,0.5,0}
 
\lstset{%
    float=hbp,%
    basicstyle=\ttfamily\footnotesize, %
    identifierstyle=\color{colIdentifier}, %
    keywordstyle=\color{colKeys}, %
    stringstyle=\color{colString}, %
    commentstyle=\color{colComments}, %
    columns=flexible, %
    tabsize=2, %
    frame=single, %
    extendedchars=true, %
    showspaces=false, %
    showstringspaces=false, %
    numbers=left, %
    numberstyle=\tiny, %
    breaklines=true, %
    backgroundcolor=\color{colBack}, %
    breakautoindent=true, %
    captionpos=b%
}
 
\author{Uwe Ziegenhagen}
\title{Titel der Präsentation}
\institute{Köln}
 
\begin{document}
\frame{
	\maketitle
}
 
\frame[containsverbatim]{
\frametitle{Titel der Folie}
\framesubtitle{Untertitel der Folie}
 
\begin{itemize}
\item Erstes Item
\item Zweites Item
\item Drittes Item
\end{itemize}
 
\lstinputlisting[language={[LaTeX]TeX},basicstyle=\ttfamily\tiny,linerange={1-12},caption={Auszug aus der Präambel dieser Präsentation},label={lis:tex01}]{\jobname.tex}
}
 
\end{document}

source1

source2

Creating MD5 files with Python

Here’s the probably last part of this series, the missing piece showing the calculation of MD5 files with Python:

# http://www.pythoncentral.io/hashing-files-with-python/
import hashlib
import os
import codecs
 
hasher = hashlib.md5()
 
def calcHash(filename):
	print("Filename:   " + filename)
	with open(filename, 'rb') as afile:
		buf = afile.read()
		hasher.update(buf)
		print("Calculated: " + hasher.hexdigest())
		md5file = (os.path.splitext(filename)[0]) + ".md5"
		writeMD5 = codecs.open(md5file, "wb", "utf-8-sig")
		writeMD5.write(hasher.hexdigest())
 
calcHash("e:/arara.log")

Using Powershell to create MD5 files

Following up on an article a few days ago here’s a script that creates a MD5 file.

# Uwe Ziegenhagen, 12.11.2014
# Reference: http://onlinemd5.com/
 
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
 
# *** Entry Point to Script ***
$file =  Get-FileName -initialDirectory "C:\"
# write-host "file: " $file
 
### Step B: Create a file handle from this string and calculate the MD5 sum
$filehandle = Get-ChildItem $file
 
write-host "Filename: " $filehandle.Name
 
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
 
write-host "Calculated Hashsum: "  $hash
 
### Step C: Write the MD5 sum from the MD5 sum file matching the other filename
$hash | out-file -FilePath $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")

SQL: Splitting a Bitmask into separate columns

Today I needed to split an integer (representing a bitmask) into its components, each component in a separate column. The following code from http://www.sqlservercentral.com/Forums/Topic1101943-392-1.aspx did the trick.

SELECT
   N,
   SIGN(N & 1) AS Bit1,
   SIGN(N & 2) AS Bit2,
   SIGN(N & 4) AS Bit3,
   SIGN(N & 8) AS Bit4,
   SIGN(N & 16) AS Bit5,
   SIGN(N & 32) AS Bit6,
   SIGN(N & 64) AS Bit7,
   SIGN(N & 128) AS Bit8
FROM (
   SELECT 511
) TestData(N)

Using Python to compute a MD5 Hash

Here some Python code to calculate the MD5 hash for a given file:

# from http://www.pythoncentral.io/hashing-files-with-python/
import hashlib
 
hasher = hashlib.md5()
with open('e:/Mappe1.xlsx', 'rb') as afile:
    buf = afile.read()
    hasher.update(buf)
print(hasher.hexdigest())

Here the final script that provides the same functionality as the Powershell script I posted earlier today:

# http://www.pythoncentral.io/hashing-files-with-python/
import hashlib
import os
import codecs
 
hasher = hashlib.md5()
 
def calcHash(filename):
	print("Filename:   " + filename)
	with open(filename, 'rb') as afile:
		buf = afile.read()
		hasher.update(buf)
		print("Calculated: " + hasher.hexdigest())
		md5file = (os.path.splitext(filename)[0]) + ".md5"
		givenMD5 = codecs.open(md5file, "r", "utf-8-sig")
		print("Provided:   " + str(givenMD5.read()).lower())
 
calcHash("e:/Mappe1.xlsx")

Using Powershell to calculate/compare MD5 hashes

Here’s some code to a) calculate a MD5 hash for a given file and b) to compare the hash with a MD5 provided in an external file. The scripts expects the MD5 file to have the same name, but MD5 extension. Credit goes to the friendly souls providing the original code, I just copied and pasted everything together.

The comparison is just visual, both MD5 hashes — the calculated and the provided one — are printed on the screen, it’s easily to extend it for an automated comparison.

# Reference: http://onlinemd5.com/
 
### Step A: Get the name of the file
# http://blogs.technet.com/b/heyscriptingguy/archive/2009/09/01/hey-scripting-guy-september-1.aspx
Function Get-FileName($initialDirectory)
{  
 [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") |
Out-Null
 
 
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "All files (*.*)| *.*"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
} #end function Get-FileName
 
# *** Entry Point to Script ***
$file =  Get-FileName -initialDirectory "H:\MD5"
 
### Step B: Create a file handle from this string and calculate the MD5 sum
 
$filehandle = Get-ChildItem $file
 
# write-host "file: " $file
# write-host "DirectoryName: " $filehandle.DirectoryName
write-host "Name: " $filehandle.Name
# write-host "pure Name: " $filehandle.BaseName
 
# http://stackoverflow.com/questions/10521061/how-to-get-an-md5-checksum-in-powershell
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($filehandle))).ToLower().Replace("-","")
 
write-host "Calculated Hashsum: "  $hash
 
### Step C: Get the MD5 sum from the MD5 sum file matching the other filename
 
$md5handle =  Get-ChildItem $($filehandle.DirectoryName + "\" + $filehandle.BaseName + ".md5")
$md5sum = [IO.File]::ReadAllText($md5handle)
 
write-host "Provided MD5        " $md5sum

Animating TikZ Graphics

After drawing the cubic Bezier a few days ago I wanted to animate it. Using the animate package this is fairly simple:

\documentclass{article}
\usepackage[paperwidth=5.5cm,paperheight=5.3cm,left=0cm,right=0cm,bottom=0cm,top=0.25cm]{geometry}
\usepackage{tikz}
\definecolor{fom}{RGB}{0,153,139}
 
\newcommand{\dat}{0.7} % 0.67
\usepackage{animate}
 
\begin{document}
 
\begin{animateinline}[poster=last, controls, palindrome]{10}
\multiframe{70}{Ry=0.1+0.01}{
\begin{tikzpicture}[x=4cm,y=4cm]
\draw[line width=1pt,lightgray] (0,0) -- (1,1);
\draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- (0,0); 
\draw (0,0) -- (0.17,0.67); 
\draw (1,1) -- (0.83,\Ry); 
 
\draw [magenta,fill=magenta](0.17,0.67) circle (.5ex); 
\draw [fom,fill=fom](0.83,\Ry) circle (.5ex); 
 
\draw[line width=1pt] (0,0) .. controls (0.17,0.67) and (0.83,\Ry) .. (1,1);
 
\node[label={[label distance=0.0cm,text depth=-1ex,rotate=90]left:Fortschritt in \%}] at (-0.1,.8) {};
\node[label={[label distance=0.0cm,text depth=-1ex]right:Zeit-Achse}] at (0,-0.05) {};
\end{tikzpicture}}
\end{animateinline}
 
\end{document}

I am animating in the TeX code (not externally) so I use animateinline with 10 frames per second. Inside this environment I prepare 70 frames, where I have the y-coordinate of the second point loop from 0.1 in steps of 0.01. The looping is controlled via Ry (that translates to \Ry in the loop). That’s it!

Here is the resulting PDF: bezier

PS: I used pdflatex from the TeX Live 2014.

Drawing Bezier Curves with TikZ

To visualize an example for cubic-Bezier CSS animations (http://cubic-bezier.com) I decided to try TikZ. That was in fact a pretty good idea, as — with the help of Google (and mostly tex.stackexchange.com) I managed to complete the following example in just a few minutes:

\documentclass{article}
\usepackage[paperwidth=5.5cm,paperheight=5.3cm,left=0cm,right=0cm,bottom=0cm,top=0.25cm]{geometry}
\usepackage{tikz}
\definecolor{fom}{RGB}{0,153,139}
\begin{document}
 
\begin{tikzpicture}[x=4cm,y=4cm]
\draw[line width=1pt,lightgray] (0,0) -- (1,1); % gray line for the linear animation path
\draw (0,0) -- (1,0) -- (1,1) -- (0,1) -- (0,0); % frame
\draw (0,0) -- (0.17,0.67); % (0,0) to 2. point
\draw (1,1) -- (0.83,0.67); % (1,1) to 3. point
 
\draw [magenta,fill=magenta](0.17,0.67) circle (.5ex); %circle 1
\draw [fom,fill=fom](0.83,0.67) circle (.5ex); % circle 2
 
\draw[line width=1pt] (0,0) .. controls (0.17,0.67) and (0.83,0.67) .. (1,1); % cubic bezier curve
 
% labels
\node[label={[label distance=0.0cm,text depth=-1ex,rotate=90]left:Fortschritt in \%}] at (-0.1,.8) {};
\node[label={[label distance=0.0cm,text depth=-1ex]right:Zeit-Achse}] at (0,-0.1) {};
\end{tikzpicture}
\end{document}

The result is pretty impressive.

bez

Windows Phone versus iPhone

Since my iPhone 4 was getting slower and I was not willing to purchase a new iPhone for hundreds of Euro I decided to give a Windows phone a try. I purchased a rarely used Nokia Lumia 630 with dual-SIM option via Amazon Marketplace for slightly more than 100 Euro.

I have been using it for a few weeks now, here’s a subjective comparison of the pros & cons:

PRO Lumia 630

Battery
The accu is not glued to the case but can simply be switched.
Price
With littler more than 100 Euro it is much cheaper if compared to the iPhone
Size
Compared with my iPhone 4 the Lumia has a bigger screen, so watching videos on the treadmill is a significantly experience
SD Card
The Lumi comes with 8GB Flash and a SD card slot accepting cards with up to 128 GB. Going from a 16 GB iPhone to 64 GB would have cost around 200 Euro for the iPhone 5.
no more iTunes
No iTunes or any other app is needed, Windows recognizes the phone as standard storage device, I can easily add content to the flash memory or the SD card.
Tiles
Not a bad idea by Microsoft to divide the start screen into tiles. It works, I guess, as good as the iPhone’s icons
Apps
The store comes with only a small part of apps if compared to the Apple or Google store. However as I do not intend to play with my phone I must confess, I have not missed anything in the app selection. One can even get offline navigation for free.
Radio
The Lumia comes with builtin radio, usable as soon as headphones are plugged in. Nice feature!

CON Lumia 630

Okay, now the Con part

Headset
My Sennheiser in-ears with builtin microphone do not work completely, I can’t adjust the volume. I could buy some new ones from Nokia, but they most likely inferior to my Sennheisers.
Audio Player
The Audio player is simply not a thought through as the corresponding Apple app. I guess it’s just a matter of user experience, but it “ain’t feel right”
Video Player
The Video Player sucks! It really sucks in two ways: It constantly nags me that a XBox account would need to be created. Microsoft, if I wanted I would but I DON’T WANT TO! It also has a technical bug: sometimes when I want to continue watching a video a click on the play button does NOTHING! Only pausing and pushing the play button again does the trick.
SMS and Call
Not sure but I haven’t found a way to call a number from which a SMS was sent. Need to investigate further…
Bluetooth Keyboards
I own two BT keyboards, one from HP, the other one the Apple BT keyboard. None is working with the Windows phone, which really sucks. Earlier versions of Windows Phone had Bluetooth HID support, Windows Phone 8.1 does not! (more info)

Conclusion

As of today I am satisfied with the Lumia, unfortunately not because it is technically superior or at least on a similar level as my iPhone, but because of the value I got for a small buck. To have a modern smartphone it’s not necessary to invest hundreds of Euros. Windows had delivered a good, but not very good, operating system that can handle everyday tasks pretty good. The Lumia 630 had good hardware, I was really suprised what is possible for 100 Euro. To get rid of the App issues I will have a closer look on the app market, I’t likely that I will be able to find simpler apps which do not bother me to create unwanted accounts.

Will I keep the Lumia: Yes, but probably not as long as my iPhone 4 which I used since 2010.