Skip to content

Instantly share code, notes, and snippets.

@tspspi

tspspi/Makefile Secret

Created June 26, 2025 12:59
Show Gist options
  • Save tspspi/138c99dc73913d047892cb420c127274 to your computer and use it in GitHub Desktop.
Save tspspi/138c99dc73913d047892cb420c127274 to your computer and use it in GitHub Desktop.
A simple example on how to use PGF in LaTeX
\documentclass[12pt,a4paper]{article}
\usepackage[inner=30mm,outer=20mm,top=25mm,bottom=25mm,headsep=10mm,footskip=12mm,includehead,includefoot,marginparwidth=15mm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgf}
\usepackage{pgfkeys}
\usepackage{pgfmath}
\title{A simple example how to use PGF plots in LaTeX}
\author{www.tspi.at}
\date{26. June 2025}
\begin{document}
\maketitle
This document is a very simple example on how to utilize our PGF
graphics in a LaTeX document that is automatically built using
GNU \texttt{make}. You can see the graphics in figure \ref{fig:sinecosine}
\begin{figure}
\input{figures/sinecosine.pgf}
\caption{Our sine and cosine PGF rendering}
\label{fig:sinecosine}
\end{figure}
\end{document}
LATEX_FLAGS = -halt-on-error -interaction=nonstopmode
LATEX = pdflatex
BIBTEX = bibtex
FIGURES_DIR := figures
FIGURES_SCRIPTS := $(wildcard $(FIGURES_DIR)/*.py)
PGF_FILES := $(FIGURES_SCRIPTS:.py=.pgf)
all: main.pdf clean
.phony: clean cleanall graphics
graphics: $(PGF_FILES)
$(FIGURES_DIR)/%.pgf: $(FIGURES_DIR)/%.py
@echo "Generating $@ from $<"
cd $(FIGURES_DIR) && python3.11 $(<F)
main.pdf: main.tex graphics
$(LATEX) $(LATEX_FLAGS) main.tex
# -$(BIBTEX) main
# $(LATEX) $(LATEX_FLAGS) main.tex
$(LATEX) $(LATEX_FLAGS) main.tex
clean:
rm -rf *.aux *.bbl *.blg *.log *.out *.toc *.lof *.lot *.ist *.glo *.acn
cleanall: clean
rm -rf main.pdf
rm -rf $(FIGURES_DIR)/*.pgf
# Before importing the plt object we have to set the PGF backend
import matplotlib as mpl
mpl.use('pgf')
# Then do imports as usual
import matplotlib.pyplot as plt
import numpy as np
# Our parameters
f = 10 # We do calculation at 10 Hz
omega = 2 * np.pi * f
t = np.linspace(0, 1, 1000) # One second period with 1000 sampling steps
A = 1.0 # Amplitude
f1 = A * np.sin(omega * t)
f2 = A * np.cos(omega * t)
fig,ax = plt.subplots()
ax.plot(t, f1, label = "Sine")
ax.plot(t, f2, label = "Cosine")
ax.grid()
ax.set_xlabel("Time [s]")
ax.set_ylabel("Function value [arb.]")
ax.legend()
plt.savefig('sinecosine.pgf', format = 'pgf')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment