diff --git a/compile_listings b/compile_listings index 23814f5..a6b7556 100755 --- a/compile_listings +++ b/compile_listings @@ -2,22 +2,30 @@ for D in listings/*; do if [ -d "${D}" ]; then # Compile without cropping. + # References to other files are interpreted relative to the + # root directory. for F in ${D}/*_page.tex; do if [ -f "${F}" ]; then echo "###### Compiling ${F} ######" - pdflatex --output-directory "${D}" "${F}" + pdflatex --shell-escape --output-directory "${D}" "${F}" fi done # Compile with cropping. + # Careful: References to other files are interpreted relative + # to the file, not to the root directory. for F in ${D}/*_crop.tex; do if [ -f "${F}" ]; then echo "###### Compiling and cropping ${F} ######" - pdflatex --output-directory "${D}" "${F}" - pdfcrop "${D}/$(basename -s .tex ${F}).pdf" \ - "${D}/$(basename -s .tex ${F}).pdf" + pushd "${D}" + pdflatex --shell-escape "$(basename $F)" + pdfcrop "$(basename -s .tex ${F}).pdf" \ + "$(basename -s .tex ${F}).pdf" + popd fi done # Compile with BibTeX and cropping. + # References to other files are interpreted relative to the + # root directory. for F in ${D}/*_bib.tex; do if [ -f "${F}" ]; then echo "###### Compiling and cropping ${F} ######" diff --git a/content/source-code-listings-minted.tex b/content/source-code-listings-minted.tex index 23441aa..049fb12 100644 --- a/content/source-code-listings-minted.tex +++ b/content/source-code-listings-minted.tex @@ -15,10 +15,7 @@ or macOS.\footnote{\url{https://docs.python-guide.org/starting/install3/osx/}} After a successful installation, you should be able to execute the aforementioned command in a terminal, confirm by pressing Enter, and see approximately the following result: -\begin{minted}[]{bash} -$ python --version -Python 3.8.5 -\end{minted} +\codeblock{bash}{listings/source-code-listings/python-version.txt} \noindent If the version number is equal to the one stated here, or higher, then everything should be set up correctly. Next, enter the command \sh{pip install Pygments}\footnote{On some operating systems, you might have to use the command \sh{pip3 install Pygments}} in the same terminal window to install the Pygments package for Python. @@ -56,19 +53,43 @@ Therefore, do not compile downloaded documents with the shell-escape flag if you We are finally ready to marvel at the aesthetic quality of the listings \pkg{minted} produces. You can define listings using a dedicated environment: -\example{lst:minted-environment}{source-code-listings/minted-environment}{Exemplary source code listing} +% Render listing directly, if minted is available. +% Otherwise, include pre-rendered images. +\ifthenelse{\equal{\listingsmode}{minted}}{% + \example{lst:minted-environment}{source-code-listings/minted-environment}{Exemplary source code listing} + }{% + \Example{lst:minted-environment}{source-code-listings/minted-environment}{source-code-listings/minted-environment_crop}{Exemplary source code listing} +} -\example{lst:minted-variants}{source-code-listings/minted-variants}{Shorthand and inline listing} +% Render listing directly, if minted is available. +% Otherwise, include pre-rendered images. +\ifthenelse{\equal{\listingsmode}{minted}}{% + \example{lst:minted-variants}{source-code-listings/minted-variants}{Shorthand and inline listing} + }{% + \Example{lst:minted-variants}{source-code-listings/minted-variants}{source-code-listings/minted-variants_crop}{Shorthand and inline listing} +} \noindent There is also a shorthand and an inline variant of the command (cf.\ \cref{lst:minted-variants}). To avoid redundancy, it may be practical to import source code directly from the source file. To accomplish this, we only have to pass the programming language and the file path to the \code{latex}{\textbackslash inputminted} command (cf.\ \cref{lst:minted-external}). -\example{lst:minted-external}{source-code-listings/minted-import}{Including from a separate file} +% Render listing directly, if minted is available. +% Otherwise, include pre-rendered images. +\ifthenelse{\equal{\listingsmode}{minted}}{% + \example{lst:minted-external}{source-code-listings/minted-import}{Including from a separate file} + }{% + \Example{lst:minted-external}{source-code-listings/minted-import}{source-code-listings/minted-import_crop}{Including from a separate file} +} \subsection{Configuring minted} Optional parameters allow us to add line numbers, line breaks, and colors. Moreover, there are numerous themes available (\cref{lst:minted-external-styled}). The introduction on Overleaf and the package documentation\footnote{Available at \url{https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted} and \url{https://ctan.kako-dev.de/macros/latex/contrib/minted/minted.pdf}, respectively.} give an extensive overview. -\example{lst:minted-external-styled}{source-code-listings/minted-import-styled}{Themes and further options} \ No newline at end of file +% Render listing directly, if minted is available. +% Otherwise, include pre-rendered images. +\ifthenelse{\equal{\listingsmode}{minted}}{% + \example{lst:minted-external-styled}{source-code-listings/minted-import-styled}{Themes and further options} + }{% + \Example{lst:minted-external-styled}{source-code-listings/minted-import-styled}{source-code-listings/minted-import-styled_crop}{Themes and further options} +} \ No newline at end of file diff --git a/exercises/source-code-listings/task.tex b/exercises/source-code-listings/task.tex index 433f1b5..f01e41b 100644 --- a/exercises/source-code-listings/task.tex +++ b/exercises/source-code-listings/task.tex @@ -30,4 +30,8 @@ If you have questions, consult the \pkg{minted} or \pkg{listings} package docume \end{enumerate} \exercisematerial{exercises/source-code-listings/source-code-listings} -\usemintedstyle{} % Reset experiments from exercise material + +% Reset experiments from exercise material if minted is used +\ifthenelse{\equal{\listingsmode}{minted}}{% + \usemintedstyle{} +}{} diff --git a/listings/source-code-listings/minted-environment_crop.pdf b/listings/source-code-listings/minted-environment_crop.pdf new file mode 100644 index 0000000..0ce2668 Binary files /dev/null and b/listings/source-code-listings/minted-environment_crop.pdf differ diff --git a/listings/source-code-listings/minted-environment_crop.tex b/listings/source-code-listings/minted-environment_crop.tex new file mode 100644 index 0000000..3de587d --- /dev/null +++ b/listings/source-code-listings/minted-environment_crop.tex @@ -0,0 +1,13 @@ +\documentclass[a4paper]{article} +\usepackage[T1]{fontenc} +\usepackage[utf8]{inputenc} +\usepackage{minted} +\begin{document} + +\thispagestyle{empty} + +\begin{minted}{haskell} + [x^2 | x <- [1..200], even x] +\end{minted} + +\end{document} \ No newline at end of file diff --git a/listings/source-code-listings/minted-import-styled_crop.pdf b/listings/source-code-listings/minted-import-styled_crop.pdf new file mode 100644 index 0000000..d06e47d Binary files /dev/null and b/listings/source-code-listings/minted-import-styled_crop.pdf differ diff --git a/listings/source-code-listings/minted-import-styled_crop.tex b/listings/source-code-listings/minted-import-styled_crop.tex new file mode 100644 index 0000000..2b35a6d --- /dev/null +++ b/listings/source-code-listings/minted-import-styled_crop.tex @@ -0,0 +1,15 @@ +\documentclass[a4paper]{article} +\usepackage[T1]{fontenc} +\usepackage[utf8]{inputenc} +\usepackage{minted} +\begin{document} +\thispagestyle{empty} + +\usemintedstyle{monokai} +\definecolor{bg}{rgb}{0.1,0.1,0.2} +\inputminted[ + breaklines=true, + bgcolor=bg +]{java}{Test.java} + +\end{document} diff --git a/listings/source-code-listings/minted-import.tex b/listings/source-code-listings/minted-import.tex index 260cfd8..d730e86 100644 --- a/listings/source-code-listings/minted-import.tex +++ b/listings/source-code-listings/minted-import.tex @@ -1,3 +1 @@ -\inputminted{java} -{listings/source-code-listings/Test.java} - +\inputminted{java}{listings/source-code-listings/Test.java} diff --git a/listings/source-code-listings/minted-import_crop.pdf b/listings/source-code-listings/minted-import_crop.pdf new file mode 100644 index 0000000..509242d Binary files /dev/null and b/listings/source-code-listings/minted-import_crop.pdf differ diff --git a/listings/source-code-listings/minted-import_crop.tex b/listings/source-code-listings/minted-import_crop.tex new file mode 100644 index 0000000..cc38e27 --- /dev/null +++ b/listings/source-code-listings/minted-import_crop.tex @@ -0,0 +1,10 @@ +\documentclass[a4paper]{article} +\usepackage[T1]{fontenc} +\usepackage[utf8]{inputenc} +\usepackage{minted} +\begin{document} +\thispagestyle{empty} + +\inputminted{java}{Test.java} + +\end{document} \ No newline at end of file diff --git a/listings/source-code-listings/minted-variants_crop.pdf b/listings/source-code-listings/minted-variants_crop.pdf new file mode 100644 index 0000000..ae69d63 Binary files /dev/null and b/listings/source-code-listings/minted-variants_crop.pdf differ diff --git a/listings/source-code-listings/minted-variants_crop.tex b/listings/source-code-listings/minted-variants_crop.tex new file mode 100644 index 0000000..9181e12 --- /dev/null +++ b/listings/source-code-listings/minted-variants_crop.tex @@ -0,0 +1,14 @@ +\documentclass[a4paper]{article} +\usepackage[T1]{fontenc} +\usepackage[utf8]{inputenc} +\usepackage{minted} +\begin{document} +\thispagestyle{empty} + +% Shorthand +Despite it is a shorthand, \mint{html}|