7

I am using TeXStudio with MiKTeX on my Windows 10 computer. I would like TeXStudio to automatically handle the code's indentation in my tex-files, so I enabled Indentation mode under the Editor settings. However, that is not sufficient, since the indentation is not always correct. After some research on the internet, I discovered that there is a useful script latexindent which ships with MiKTeX.

How am I supposed to use latexindent? Is it 'activated' automatically while TeXing or is there some key combination to run the script? I am a bit confused for the moment and also surprised that I cannot find any documentation about this topic.

8
  • Could you be more specific about "handling" indentation? Because as far as I know, indentation mode in TXS is only regarding code (i.e. Copy and paste, and automatic indentation when hitting enter on an indented line) so maybe its unrelated. Oct 15, 2016 at 23:44
  • 1
    latexindent is not magical. It is a short script that you need to call. just like you do with BibTeX, or biber, or makeindex. Have a look at the documentation to find more information.
    – Johannes_B
    Oct 16, 2016 at 13:13
  • Maybe the video "latexindent.pl demonstration (using arara)" helps.
    – gernot
    Oct 16, 2016 at 13:21
  • @GuilhermeZ.Santos: I am talking about the code in my tex-files. "handling" indentation is what you see in the video that gernot refers to: the correct indentation is automatically applied while the user types.
    – Jeroen
    Oct 16, 2016 at 17:16
  • @Johannes_B: yes, but how do I "call" the script? I have read the documentation several times, but I still don't get it...
    – Jeroen
    Oct 16, 2016 at 17:17

1 Answer 1

11

The following works for me without any other necessities, such as arara.

  1. Install latexindent via MiKTeX Console > Packages.
  2. Download latexindent on CTAN (zip-file).
  3. Navigate to C:\Program Files\MiKTeX 2.9\scripts\latexindent and replace the files in there with the files from the downloaded zip-file in step 2.
  4. Create a new User Command in TeXStudio > Options > Build: "C:/Programs Files/MiKTeX 2.9/scripts/latexindent/latexindent.exe" -w -s %.tex

User Command

Calling this new command via Tools > User, should run latexindent and indent your file! You can even assign a shortcut to the User Command at TeXStudio > Options > Shortcuts to make things quicker.

Note: As Johannes_B pointed out in the comments above (thanks!) and as mentioned in TeXStudio's manual, the % just refers to the basename, i.e. the file name without the file extension. But the extension is needed for the script. If you also want to use latexindent for other extensions, you can replace %.tex by ?c:me", which is equivalent to the current document's filename with the extension attached. To conclude, if the backup files generated by -w annoy you, you can use -s -o ?c:me" ?c:me" instead; this will directly override your tex-file without backups.

8
  • If MikTeX installs the script, you don't need to download from CTAN.
    – Johannes_B
    Oct 17, 2016 at 16:54
  • 1
    @Johannes_B: In my fresh installation, MiKTeX 2.9 only included defaultSettings.yaml, indent.yaml and latexindent.pl. Unless one installs Perl and uses the pl file instead of an exe, my solution therefore doesn't work.
    – Jeroen
    Oct 17, 2016 at 16:58
  • Oh, MikTeX doesn t include the exe? That is bad. :-(
    – Johannes_B
    Oct 17, 2016 at 17:39
  • 1
    Thanks for letting me know of this -- I'll contact the MikTeX maintainer and ask them to fix this.
    – cmhughes
    Oct 18, 2016 at 7:50
  • 1
    for reference, I contacted the maintainer today -- not sure what the turn around time is. (I'm working on latexindent v3.0 currently)
    – cmhughes
    Oct 19, 2016 at 17:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .