18

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.

migrated from stackoverflow.com Oct 5 '11 at 19:09

This question came from our site for professional and enthusiast programmers.

  • @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 5 '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 Oct 5 '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 5 '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 5 '11 at 19:17
  • @Werner I'm interested in an automatic code fixer upper which also aids in avoiding potential formatting problems. – puk Feb 2 '12 at 6:27
4

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/

5

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

0

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 $

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.