Author Archive

Mit Python XML Tags aus XML-Dateien löschen

Kürzlich musste ich XML-Tags aus XML-Dateien löschen, um die entsprechenden XML-Dateien etwas übersichtlicher zu gestalten. Der richtige Weg wäre sicher gewesen, einen XSLT-Prozessor zu nutzen, der die entsprechenden Tags ausfiltert, aber mangels Zeit habe ich dann doch ein kleines Python-Skript gebaut. Die zu entfernenden Tags hatten auch keine Properties und ließen sich daher gut entfernen.

def filter(oldfile, newfile, filterStart, filterEnd):
    killFlag = 0
    with open(newfile, 'w') as outfile, open(oldfile, 'r', encoding='utf-8') as infile:
        for line in infile:
            strIndex = line.find(filterStart)
            if (strIndex > -1) | (killFlag == 1):
                killFlag = 1
            else:
                outfile.write(line)
                strIndex2 = line.find(filterEnd)
                if (strIndex2 > -1):
                    killFlag = 0
 
filter('somexmlfile.xml', 'somefilteredxml.xml', '<xs:annotation>', '</xs:annotation>')

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Gesamtfolienzahl in Powerpoint einfügen

Als TeXie mag man es nicht glauben, aber in Powerpoint gibt es keine eingebaute Möglichkeit, die Gesamtzahl der Folien auf der Folie selbst auszugeben. Über den Umweg VBA geht es (gefunden unter https://superuser.com/questions/130489/insert-total-number-of-slides-in-powerpoint-2007)


Sub numberSlides()
' https://superuser.com/questions/130489/insert-total-number-of-slides-in-powerpoint-2007
' run with F5

Dim s As Slide
Dim shp As Shape

For Each s In ActivePresentation.Slides
s.DisplayMasterShapes = True
s.HeadersFooters.SlideNumber.Visible = msoTrue

For Each shp In s.Shapes
If Left(shp.Name, 12) = "Slide Number" Then
shp.TextFrame.TextRange.Text = s.SlideNumber & " von " & ActivePresentation.Slides.Count
End If
Next
Next
End Sub

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

LaTeX für Geisteswissenschaftler: Der nächtliche Build-Prozess

Vor einer Weile haben sich ein paar TeXies zusammengefunden, um gemeinsam ein Skript/Buch zum Thema „LaTeX für Geisteswissenschaftler“ zu schreiben. Das Skript sowie alle Dateien ist auf github verfügbar. Ich schreibe das Kapitel zu git und SVN (aktuell noch auf der TODO-Liste) und kümmere mich um den nächtlichen Build.

  1. Ich habe hier einen kleinen Intel Nuc (Intel DN2820FYKH) mit Celeron Prozessor, der unter Ubuntu läuft.
  2. Auf der Maschine läuft Jenkins, ein Java-basiertes Build-Tool
  3. In Jenkins habe ich einen Workflow definiert, der:
    • das Arbeitsverzeichnis löscht
    • alle Dateien aus Github auscheckt
    • LuaLaTeX, Biblatex, LuaLaTex über den Code laufen und das Ergebnis in das „FinalesPDF“ Verzeichnis packt.
  4. Wenn der Build erfolgreich war, wird zusätzlich eine Blinkstick LED (https://www.blinkstick.com) auf grün gesetzt, sonst auf rot. Wenn ich morgens ins Wohnzimmer komme, sehe ich so auf einen Blick, ob es Probleme gab oder nicht.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Mehr zu Emacs

Nachdem mir ein Kollege in der Firma gezeigt haben, wie er Emacs produktiv einsetzt, wird es jetzt Zeit, Emacs deutlich intensiver und ausführlicher zu behandeln. Meine Erfahrungen werde ich in einem Skript sammeln, das passende Github-Repo für den TeX-Code habe ich bereits angelegt. Wer mitmachen möchte, kann sich gern bei mir melden.

Github Repo

Hier noch ein empfehlenswerter Link zu vielen Emacs-Themen: https://github.com/emacs-tw/awesome-emacs

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Neue Version des varsfromjobname Pakets

Letzten Montag habe ich die neue Version meines varsfromjobname Pakets auf CTAN hochgeladen, es gibt auch ein Github Repo dazu: https://github.com/UweZiegenhagen/VarsFromJobname

What does this package do?

Using varsfromjobname.sty one can easily extract information from the filename, if it follows a certain form.

The standard LaTeX command \jobname returns the name of the LaTeX (master) file. If the filename consists of tokens separated by hyphen we can easily extract certain tokens that can be used inside the document.

The package expects the filename to be of the form one-two-three-four-five-six-seven-eight-nine.tex and offers the following commands:


\getfromjobname{param}, with param in the range of 1 to 9 (a highlevel interface to the following commands)

\getonefromjobname
\gettwofromjobname
\getthreefromjobname
\getfourfromjobname
\getfivefromjobname
\getsixfromjobname
\getsevenfromjobname
\geteightfromjobname
\getninefromjobname

Why can it be useful?

You can use this package for example to define the date of the document in the filename, personally I use it e.g. for scrlttr2 letters.

Change History

  • Version 0.5, as of January 11th 2009: Initial version published
  • Version 1.0, to be published May 2017: Added various commands which extract necessary information also from included documents. Modified code as suggested by egreg

Shortcomings

  • \getfromjobname is not fully expandable, use one of the other commands if you need expansion
  • Version 1.0 cannot extract information from included sub-documents
  • The package cannot handle more than nine tokens, see the reference to egreg’s answer on TSX below for an updated version

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Folien vom Informatiktag 2017 in Wuppertal

Hier meine Folien vom 16. Informatiktag in Wuppertal sowie ein Beispiel für die Erstellung von Klausurblättern mit LaTeX.

Wuppertal Folien (PDF)

exam Beispiel (TeX)

exam Beispiel (PDF)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Approximating Pi with Python

One of the many approximations of Pi is the Gregory-Leibniz series (image source Wikipedia):

Leibnis Series for Pi

Here is an implementation in Python:

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 06:52:11 2017
@author: Uwe Ziegenhagen
"""
 
sum = 0
factor = -1
 
for i in range(1, 640000, 2):
        sum = sum + factor * 1/i
        factor *= -1
        # print(sum)
 
print(-4*sum)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Russische Bauernmultiplikation mit Python

Durch eine SX Frage bin ich auf das Verfahren der „Russischen Bauernmultiplikation“ gestoßen, mit der man ohne Multiplikation ganzzahlige Zahlen miteinander multiplizieren kann. Just for Fun hier die Python-Implementierung:

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 18 10:04:40 2017
 
@author: Uwe Ziegenhagen
"""
import pandas as pd
from math import floor
 
def russianPeasantMultiply(a, b):
    assert a > 1
    assert b > 0    
    data = pd.DataFrame([[a, b]], columns=list('ab'))
    while a > 1:
        a = floor(a/2)
        b = b + b
        data.loc[len(data)]=[a, b]
    data = data[data['a'] % 2 == 1]    
    return(data.b.sum())
 
print(russianPeasantMultiply(63, 17))

Ohne pandas geht es sicher auch, aber pandas macht es etwas einfacher…

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Generating a normal distribution table with SciPy

Here’s a simple example how one can generate a normal distribution table with Python and scipy so that it can be imported into LaTeX.

Example-03.zip

# -*- coding: utf-8 -*-
"""
Created on Mon Mar 13 21:14:17 2017
@author: Uwe Ziegenhagen, ziegenhagen@gmail.com
 
Creates a CDF table for the standard normal distribution
 
use booktabs package in the preamble and put 
the generated numbers inside (use only one backslash!)
 
\\begin{tabular}{r|cccccccccc} \\toprule
<output here>
\\end{tabular}
"""
 
from scipy.stats import norm
 
print(norm.pdf(0))
print(norm.cdf(0),'\r\n')
 
horizontal = range(0,10,1)
vertikal = range(0,37)
 
header = ''
for i in horizontal:
    header = header + '& ' + str(i/100)
 
print(header, '\\\\ \\midrule')
 
for j in vertikal:  
    x = j/10
    print('\\\\', x)
    for i in horizontal:
        y = x + i/100
        print('& ', "{:10.4f}".format(norm.cdf(y),4))
 
 
print('\\\\ \\bottomrule \r\n')

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Win32 Dialoge mit Python auswerten

http://stackoverflow.com/questions/4485610/python-message-box-without-huge-library-dependancy zeigt, wie man Windows Standard-Dialoge (wie MessageBox und JaNeinAbbrechen) mit Python auswerten kann:

# using ctypes
import ctypes
MessageBox = ctypes.windll.user32.MessageBoxW
MessageBox(None, 'Hello World', 'This is the window title', 0)
 
# using win32ui
import win32ui
win32ui.MessageBox('This is the message', 'Window Title')
 
# using win32con
import win32con
 
result = win32ui.MessageBox('The Message', 'The Title', win32con.MB_YESNOCANCEL)
 
if result == win32con.IDYES:
    win32ui.MessageBox('You pressed "Yes"')
elif result == win32con.IDNO:
    win32ui.MessageBox('You pressed "No"')
elif result == win32con.IDCANCEL:    
    win32ui.MessageBox('You pressed "Cancel"')

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website