32

I was looking for this a while back for JavaScript, but I was wondering a general purpose one exists for all, or most languages.

For example for LaTeX I would put the following in a text box

$f_i^k+10=x$

and it spits out the better formatted version

%%%
%% Insert comment describing function here
%%%
$ f_{i}^{k} + 10 = x $

I can't be the only person on the planet that does not wish to go through a massive .tex file and fix these tedious problems.

doncherry
  • 54,637
puk
  • 3,061
  • 5
  • 30
  • 41
  • @dmckee this is too funny, on the linke bdares provided, someone has further complained "This should probably be community wiki". I imagine if I checked the community wiki it will claim that it should probably belong on a blog... – puk Oct 05 '11 at 18:30
  • There is no guarantee that a question will find a good fit on the Stack Exchange network. In any case bdares link is not specifically about pretty printing now about literate programming/in-line documentation, so it may or may not be a duplicate. I'm not active enough on TeX.SE to be certain. – dmckee --- ex-moderator kitten Oct 05 '11 at 18:34
  • @puk You misunderstand the comment. It merely means that the answers should belong to the community, instead of to individual users (it’s a Stack Overflow feature). The question bdares links to is fine, and so is yours. – Konrad Rudolph Oct 05 '11 at 18:34
  • 2
    @puk: Are you interested in "correcting" your LaTeX code (in the .tex file) so that super-/subscripts are actually put in braces { }, thereby possibly avoiding formatting problems? – Werner Oct 05 '11 at 19:17
  • @Werner I'm interested in an automatic code fixer upper which also aids in avoiding potential formatting problems. – puk Feb 02 '12 at 06:27
  • @puk: Since super/subscripts may contain expressions and are therefore not limited to single character elements, it would be difficult to generalize code clean-up. sed might be able to do some of the grouping based on certain assumptions. But such rules would be source-specific; again, not very general. – Werner Feb 02 '12 at 06:33
  • I think you could use blacktex on the lines of black formatter for python by Nico Schlömer. See here: https://tex.stackexchange.com/a/602715/ – Tejas Shetty Jun 26 '21 at 09:36
  • This should be linked with https://tex.stackexchange.com/q/100/86 – Andrew Stacey Jan 31 '22 at 06:32

5 Answers5

8

I created a website that formats the latex code to make indents look correct.

The general idea of the website is to make sure you can read the code. It also provides table indentations. I am still looking to see if adding an empty comment block for formulas is possible.

https://c.albert-thompson.com/latex-pretty/

Whitecat
  • 1,321
6

Take a look at TeXpretty. I have used it a couple of times for cleaning up messy code and it does a decent job.

Martin Heller
  • 11,391
6

As of Version 3.7, latexindent.pl can help with this.

As a warning: do take care when using regular expressions such as those below, always check that they are behaving as you would like, and test them before using them on anything important.

Starting with the following sample code

\begin{env}
$f_i^k+10=x$
\end{env}
$g_ij^12-3=x$

and the YAML file, say puk1.yaml

replacements:
  -
    substitution: |-
      s/\$(.*?)\$/%%
            %% Insert comment describing function here
            %%
            \$$1\$/sxg

and running the command

latexindent.pl -r myfile.tex -l=puk1.yaml

gives the output:

\begin{env}
    %%
    %% Insert comment describing function here
    %%
    $f_i^k+10=x$
\end{env}
%%
%% Insert comment describing function here
%%
$g_ij^12-3=x$

This doesn't have everything you requested. We can incorporate some more replacements in the following file, say puk2.yaml

replacements:
  -
    substitution: |-
      s/\$\h*(.*?)\h*\$/#
            my $comment = "%%\n%% Insert comment describing function here\n%%\n";
            my $body = $1;
            # add braces to superscripts and subscripts
            $body =~ s@(\^|\_)([a-zA-Z0-9]+)@$1\{$2\}@sg;
            # add a single space around + - =
            $body =~ s@\h*([+\-=])\h*@ $1 @sg;
            # put it all together
            $comment."\$ ".$body." \$";/sxge

and running

latexindent.pl -r myfile.tex -l=puk2.yaml

gives

\begin{env}
    %%
    %% Insert comment describing function here
    %%
    $ f_{i}^{k} + 10 = x $
\end{env}
%%
%% Insert comment describing function here
%%
$ g_{ij}^{12} - 3 = x $
cmhughes
  • 100,947
2

If you're on VS code, try latex-formatter.

1

this online formatter could help you.

asouqi
  • 49
  • 2