8

I use the packages listings and minted for my syntax highlighting in LaTeX.

My problem is that my indent is way too large (normal tabulator) and I don't want to use space. Is it possible to change the tabsize here? Something like tabsize=2 don't work.

And is it possible to auto indent my programcode? I mean, that listings (or any other package) recognize if-statements and so on and auto indent my whole code.

Here is a short example:

\documentclass{article}
\usepackage{listings}
\usepackage{minted}

\begin{document}
\begin{minted}{bash}
#!/bin/bash

parameter1=$1

#some commentary
if [ "$parameter1" == "-h" ] || [ "$parameter1" == "--help" ] ; 
then 
echo my helpfile
else
#the rest of my programcode
\end{minted}
\end{document}

The if-statement should look something like this:

if [ "$parameter1" == "-h" ] || [ "$parameter1" == "--help" ] ; 
  then 
  echo my helpfile
else
  #the rest of my programcode
2
  • Welcome to TeX.SX! Please help us help you and add a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – CarLaTeX
    Jan 26 '17 at 20:16
  • I edited a short example in my startpost. Jan 27 '17 at 13:21
8

You can use some optional arguments to your minted-environment. I added [obeytabs=true,tabsize=2] to your example, as well as Tabstops for the if and else block. Please have a close look at the indentation in the input file and in the output.

Please read the manual of the minted-package especially the note to the obeyetabs-option.

(If this answer doesn't suit you, you'll have to use another pretty printer (maybe an SED-script), to mangle your code into the look you want, before feeding that into LaTeX and minted. I don't know of any LaTeX-package which should be able to format and prettyprint your original code.)

Note: I had to replace the tabs of my input file by spaces for the sake of TeX.SE, to resemble the look of my input file.

\documentclass{article}
\usepackage{listings}
\usepackage{minted}

\begin{document}
\begin{minted}[obeytabs=true,tabsize=2]{bash}
#!/bin/bash

parameter1=$1

#some commentary
if [ "$parameter1" == "-h" ] || [ "$parameter1" == "--help" ] ; 
then 
         echo my helpfile
else
         #the rest of my programcode
fi
\end{minted}
\end{document}

This is the result:

enter image description here

4
  • Works great! May you can also help me, because the text goes beyond the right edge. For example, the text "This very large text is very large" looks like "This very large text is ver" Jan 27 '17 at 14:55
  • In that case, you can decrease the text size of the listed code (to some degree). But you can't decrease it to unreadable size ... In that case an external pretty printer tool would be of great help.
    – Jan
    Jan 27 '17 at 14:58
  • Found out, that you can use the breaklines=trueoption, so \begin{minted}[breaklines=true,obeytabs=true,tabsize=2]{bash} is the key. But i don't like the arrows after every break. Huh. And also i get an auto indent with every break. And this is false. Jan 27 '17 at 15:31
  • 1
    Solution for anybody else: Use breaksymbolleft \begin{minted}[breaklines,obeytabs=true,tabsize=2,breaksymbolleft=]{bash} everything works now! Jan 27 '17 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.