in Latex

Using “minted” package in Latex to format programming codes on Windows machines

I was looking for a package to format programming code inside \textrm{\LaTeX} document for a specific programming language. There are two popular options, which are minted and listings. Between the two options, I found minted more flexible and easy to use, of course only when installed. It allows you to easily specify the programming language of your interest, to format margins, line spacing, footnoting, reading from an actual code file, and other things. I am a Windows user (unfortunately) and installing minted is not as easy as you might think. I found no comprehensive guidelines to install this package and get to use it on Windows machine. In this post, I will try my best to explain the whole process step-by-step without any buzz word and confusion. By the way, I tried these steps on Windows 10, not sure if it is applicable in other versions.

Step 1: install python. It seems weird but it is true. The reason is that the minted package uses special library/definition of colors in Pygments package through python. I installed Python 2.7, but I am not sure if the same process would work for Python 3X.

Step 2: Go to the folder in which you installed Python, usually, it is C:\Python27. Then, create a new folder and call it “Scripts”. From now on, I will refer to “C:\Python27” and “C:\Python27\Scripts” in the rest of the guidelines. If you install a different version in a different place on your machine, change it accordingly.

Step 3: Push Windows key (between left Ctrl and Alt buttons) and type “System”. Then a window pops up where you should click on “Advanced system settings”, which takes you to a new window. There, click on “Advanced” tab, and select “Environment Variables…” at the bottom. In the new window, you will see two sections: in the “System variables” section, find “Path”, click on it and then select “Edit…” in the bottom of the section. In the new window, you should create new paths to Python and Python\Scripts. Select “New” and type “C:\Python27”. Do the same for “C:\Python27\Scripts”. Then, click “Ok” three times.

To check if the two paths are recognised, open a command window (push Windows key and type “cmd”, then better to right click on it and select “run as administrator”) and run SET. You should see the two new paths in the results. If not, you probably need to restart your computer and check again.

Step 4: To check if you installed python appropriately, open a command window (or use the same you already have opened) and run python --version. You should see Python 2.7 (as I installed this version). If you see an error instead of the version of Python on your machine, consult with this page.

Step 5: Let’s see if you have an appropriate version of pip on your machine. Run pip --version in the command window. It should return the version of the pip installed on your machine. If you get an error, you have to install pip by downloading get-pip.py from here, or any other websites. Save the file somewhere outside of Windows environment path. Go to that folder in the command window (using cd command, for instance c:\Users\uqapourm\Downloads) and run python get-pip.py. That should install pip on your machine. The good thing is that it will automatically install setuptools for Python.

If you already have pip on your machine, and not setuptools, you should download it in a zip file from here (at this moment, it is named “setuptools-38.5.1.zip”). Unzip the package in a folder outside of Windows environment paths and Python installation path. Then, go to that folder in the command window (remember you should have started command window as an administrator when installing things in there) and run python easy_install.py.

To check the appropriate installation of all the packages so far, run python -m pip install --upgrade pip setuptools wheel in the command line. You should see either up-to-date message for the packages or updating the packages on the command line.

Step 6: Now, we have to install Pygments, as we went this far just to be able to install this package. To do so, run pip install Pygments on the command line. It should be installed easily, you can check successful installation by running pygmentize --version where you should see the version of the package successfully in the command window. Alternatively, you can check Python installation folder (in my case, it is C:\Python27\Scripts) where you should see pygmentize.exe.

Step 7: This step is to allow minted package an access to pygmentize.exe from your preferred \textrm{\LaTeX} editor. To do so, go to Windows root (press Windows key and type %SystemRoot%" or directly go to “C:\Windows”). Create a text file and rename it to “pygmentize.cmd”. Open the file with a text editor (such as Notepad) and write the following code in there and save it:
@echo off
set PYTHONPATH=C:\Python27
%PYTHONPATH%\python.exe %PYTHONPATH%\Scripts\pygmentize.exe %*

We are done! Everything is configured appropriately. There is only one more thing to do. Since minted package requires access to the shell, this should be permitted within your \textrm{\LaTeX} editor. To do so, you have to add -shell-escape to your command list for LaTex, PdfLaTex, XeLaTex, and LuaLaTex in your favourite tex editor. I use “TexStudio” where I can modify those commands by going into “Options > Configure TexStudio > Commands” where I changed the following commands by adding -shell-escape:
latex.exe -shell-escape -src -interaction=nonstopmode %.tex
pdflatex.exe -shell-escape -synctex=1 -interaction=nonstopmode %.tex
xelatex.exe -shell-escape -synctex=1 -interaction=nonstopmode %.tex
lualatex.exe -shell-escape -synctex=1 -interaction=nonstopmode %.tex

Now, it should be all good. You can try this minimal example as a test to make sure everything is working as it should.

\documentclass[10pt,a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8x]{inputenc}
 
\usepackage{minted}
 
\begin{document}
\begin{minted}{python}
import numpy as np
 
def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable
 
    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1) 
 
    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;
 
                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)
 
                VT = np.zeros((n*m,1), int)
 
    return M
\end{minted}
\end{document}

You can also save the code in a tex file (let’s say you named it “testMinted.tex”), and compile it from command line by going in to the folder where the file resides (let’s say it is in “C:\Users\uqapourm\Desktop\testMinted”) and running pdflatex.exe -shell-escape -synctex=1 -interaction=nonstopmode testMinted.tex in the command line.

P.S. 1: Sometimes, you need to clear the cache in order to get the package to work. This will slow down the \textrm{\LaTeX} operation, but it will avoid errors. To do so, change permeability to \usepackage[cache=false]{minted}.

P.S. 2: If you are using “TexStudio”, then new packages should be installed automatically. If not, you can install minted package directly, by pressing Windows key, type “MiKTeX Package Manager” and then find “minted” from the list, right-click on it and push install button. If your preferred repository is down for some reason, then you should go to “repository” tab, select “Change Package Repository”, push “Next” and select another online repository from the list.

Share if you liked:

Comments

Comment