In march 2015, I wrote about using Pygments when including source code in papers (and on the web). I still use Pygments, but I have never updated the description on how I do it now. In most cases, I use Pygments implicit without interacting with the program myself. I leave it to the LaTeX package minted. You still need to install Pygments, but you do not need to interact with it. The minted package calls Pygments to do the task for you. The only thing you have to do is to ensure that LaTeX or PDFLaTeX (or whatever version of LaTeX you are using) is passed the --shell-escape option. Otherwise, LaTeX is not allowed to do such calls.
I often use minted in combination with the LaTeX package fancyvrb. For example in this excerption from a LaTeX document:
\usepackage{fancyvrb}
\usepackage{minted}
⁝
The function \mintinline{python}{dblderivative} should be used
like this:
\inputminted[firstline=2]{python}{dblderivative-ex.py}
The execution of this example will give the following output:
\VerbatimInput[label=Resultat fra kjøring]{dblderivative-ex.out}
In the preamble I load the two LaTeX packages. The \inputminted command will include the Python code from the file dblderivative.py pretty-printed in the document in the selected style. You can use the command \usemintedstyle to change the style of the code included in the document. To list possible styles run the following command in a shell:
pygmentize -L styles
The default style is inspired by Emacs. The \VerbatimInput command in the example will include the text file dblderivative-ex.out with a nice layout in the document, including a descriptive text (label). In this example the text file is generated from running the Python program dblderivative-ex.py piping the output from the program to the text file:
python dblderivative-ex.py > dblderivative-ex.out
This example above can typically end up looking like this in the final PDF-document:
You can also include code directly in LaTeX using the minted environment:
I have Python 3.7 on my Mac (currently, I am not able to get Qt and Python 3.8 to work). I have also installed numpy and matplotlib with pip (since I have many different Python installations on my Mac, including 2.7, 3.7 and 3.8, I will use pip3.7 to be sure the right Python installation is used):
> cd path/to/my/git/clone/of/qt
> git clone git://code.qt.io/qt/qt5.git
> cd gt5
> git checkout 5.12
> perl init-repository
> cd ..
> mkdir qt5-build
> cd qt5-build
> ../qt5/configure -developer-build -opensource \
-nomake examples -nomake tests
> make -j4
In the configure step above you have to accept the license of the software.
It is also possible to download the source code directly, without using git, at http://download.qt.io/official_releases/qt/. In the following example I installed Qt 5.13.2 on my Mac. First, I downloaded the qt-opensource-mac-x64-5.13.2.dmg file from the URL above. Then I used the application qt-opensource-mac-x64-5.13.2.app to install the tools and the source code at my home directory in the folder Qt5.13.2. Finally, I performed the following steps (again, you have to accept to license in the configure step):
> cd ~/Qt5.13.2/5.13.2/Src/
> ./configure -prefix $PWD/qtbase -opensource -nomake tests
> make -j 4
The next step is to install PySide2 using pip:
> pip3.7 install PySide2
A couple Python Qt examples
Once all is installed you can create your own first Qt application:
How to use Matplotlib to plot graphs in a Python Qt application
This has been fun, but no plotting of graphs so far. How can we connect Matplotlib with Qt? We start with this code:
# Qt and matplotlib integrationimportmatplotlibmatplotlib.use("Qt5Agg")frommatplotlib.backends.backend_qt5aggimportFigureCanvasfrommatplotlib.figureimportFigure
We use matplotlib.use("Qt5Agg") to select the Qt backend for rendering and GUI integration. FigureCanvas is the area onto which the figure is drawn. Since we are creating a Qt application where we want to plot a graph in the GUI, we have to use the FigurCanvas from Qt. A Matplotlib Figure includes the plot elements. We will in this example add a plot to such a Figure (see add_subplot below).
We will use different Qt widgets in the implementation of this example. QLabel and QLineEdit for text, QPushButton for buttons, and QHBoxLayout and QVBoxLayout to create the layout of the application:
# Use Qt with the PySide2 mapping (Qt's official Python mapping)fromPySide2.QtWidgetsimportQApplication,QWidgetfromPySide2.QtWidgetsimportQHBoxLayout,QVBoxLayoutfromPySide2.QtWidgetsimportQPushButton,QLabel,QLineEdit
We will create a widget, MyPlotWidget, for our plotting application. It includes a canvas for the plot:
classMyPlotWidget(QWidget):labeltext="Write an expression to plot (Python "+ \
"syntax, including numpy mathematical functions ""with one variable): "def__init__(self,app):QWidget.__init__(self)self.fig=Figure(figsize=(10,8),dpi=100,facecolor=(0.8,0.8,0.9),edgecolor=(0,0,0))self.ax=self.fig.add_subplot(1,1,1)self.canvas=FigureCanvas(self.fig)
Then we create all the building blocks for the application, including input text (QLineEdit), buttons (QPushButton) and help text (QLabel):
# Create the building blocksself.setWindowTitle("Plot a graph")self.plotlabel=QLabel(self.labeltext)self.exprlabel=QLabel("Expression (with variable x):")self.expr=QLineEdit("sin(1/x)")self.minlabel=QLabel("Min x:")self.min=QLineEdit("-pi/4")self.min.setFixedWidth(80)self.maxlabel=QLabel("Max x:")self.max=QLineEdit("pi/4")self.max.setFixedWidth(80)self.button1=QPushButton("Plot it")self.button2=QPushButton("Exit")
The overall application layout is specified by at QVBoxLayout, a vertical stack of building blocks (widgets). Inside this box we also have a horizontal line of building blocks created with a QHBoxLayout:
The final thing we do when a MyPlotWidget is initialised is to connect the GUI-actions to the code (button clicks, and carriage return in the edit fields):
# Connect button clicks and CR to actionsself.button1.clicked.connect(self.plotit)self.button2.clicked.connect(app.exit)self.expr.returnPressed.connect(self.plotit)self.min.returnPressed.connect(self.plotit)self.max.returnPressed.connect(self.plotit)
The method plotit calculates 100.001 points on the graph for the given x-values and the expression (function). It then clears the plot and updates it with the calculated values. Finally the updated plot is drawn in the canvas:
That was the complete implementation of the MyPlotWidget class. The last lines of code creates a Qt application with such a visible widget before the execution is handed over to the Qt execution environment:
# Create Qt app and widgetapp=QApplication(sys.argv)widget=MyPlotWidget(app)widget.show()sys.exit(app.exec_())
The complete code for this Qt and Matplotlib example is available here: qt-matplotlib.py.