[Tex/LaTex] Change tabsize and auto indent (package: listings/minted)

listingsminted

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

Best Answer

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