1

Maybe it's the writer (or the editor) who should be capable of this. But anyway, I wonder if I can do that with TeX.

I usually want my TeX-code to appear somehow justified. Is there a way to do that?

Precisely I would like to avoid the chaos:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 
Ut purus elit, vestibulum ut, 
placerat ac,  $\int$ 
adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,
magna. 

and want to find a way so that my code automatically looks more or less justified (after I wrote most of it):

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut 
purus elit, vestibulum ut, placerat ac, $\int$ adipiscing vitae,
felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy 
eget, consectetuer id, vulputate a, magna. Donec vehicula augue 

I'm using Kile (but I wouldn't mind to use another just to open the code there and then work back in Kile).

5
  • 9
    You are asking a question about an editor but not said what editor you are using M-q will do that in emacs for example Jul 4, 2016 at 10:02
  • 3
    [personal opinion] Since justification of your source file and of your generated document won't match, I'm circumspect about justifying in your IDE. I personally use the one sentence per line approach, that, if not mandatory, is easier to use with version-control and make easy to find again the line of code related to the part of your document you'd like to change.
    – ebosi
    Jul 4, 2016 at 10:09
  • Maybe is too late for one sentence per line approach, since my document is written.
    – c.p.
    Jul 4, 2016 at 10:50
  • Any editor will show the text the way you want if you use a "one paragraph per line" approach. That is to delete all the newline codes at the end of each line in either 1st or 2nd code above. This can be done with a succession of sed or tr or awk commands or python script etc, whatever you're comfortable with. Same thing if you want a "one sentence per line" (which will not lead to a nearly justified look).
    – alwaysask
    Jul 4, 2016 at 13:49

2 Answers 2

2

I installed emacs, opened my .tex document there and used M-q as in the comments was suggested. In principle, it works. But M-q doesn't care about the %-commented lines, so that approach is not 100% safe.

Otherwise, installing AUCTeX, it works.

1
  • 3
    »But M-q doesn't care about the %-commented lines« – it does when you use AUCTeX with emacs.
    – cgnieder
    Jul 4, 2016 at 10:54
1

I made a python script to convert sloppy input text (like the "chaos" example above) into a "one paragraph per line" text that will be displayed almost justified (word-wrapped) by any editor. Also useful when the text is extracted from pdf files or imported from other software.

I wanted to keep it as simple as possible and easy to understand/modify by anyone without python knowledge (like me :)).

Notes: Please do not use on preambles or other TeX command blocks. Extract only the relevant text on a separate file. Please backup your work before using the script!!

The python script (not tested on windows hosts or texts):

#
# script name:      oneParagraphPerLine.py
# python version:   2 or 3 
# usage:            python oneParagraphPerLine.py infile # this will print the output to screen
# usage:            python oneParagraphPerLine.py infile >> outfile # this will redirect the output to "outfile"
# description:      this script converts sloppy input text into a "paragraph per line" text (mostly in TeX source files) 
#                   also usefull when importing text from other software or extracting text from pdfs.
#                   don't run it on preamble or heavy [La]TeX code, it will mess up things!
#

import sys

try:
    infile = sys.argv[1]
except IndexError:                           # stop if no infile given
    print ('Please provide a file to convert.') 
    exit()

with open(infile) as file:
    newline = True                           # an indicator showing if the last line printed ended with a CR
    for line in file:
        line = line.rstrip()                 # remove trailing whitespaces
        if (
            line.startswith(('\\', '$'))     # for lines starting with a command, or inline math; add comma sepparated strings
            or line == ""                    # or empty lines
            or "%" in line                   # or lines with comments (it will trigger on \% though)
            ):
            if not newline: sys.stdout.write('\n') # if the last line was not ending with a CR, insert one
            sys.stdout.write(line + '\n')    # print the line (it has a CR)
            newline = True                   # update the indicator
        else:
            line = line.replace('\n', '').replace('\r', '')  # remove linux and windows CR, LF
            sys.stdout.write(line + ' ')     # print the line with a space after it (no CR)
            newline = False                  # tell the indicator we printed a line without a CR
if not newline: sys.stdout.write('\n')       # in case last line printed had no CR
# that's it

The test input file was based on the "chaos" example above:

\documentclass{article}
\usepackage{amsmath}

 \newcommand\multiline{
     It will mess up multiline commands, better don't try!!!
     }

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.    
Ut purus elit, vestibulum ut,    
placerat ac,  $\int$      
adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a,  
magna. 


Two empty lines above test.

A line with a comment % comment
should remain as it is but   
it will trigger on percent signs,
like 100\%,   
as well because there might be lines with both 100\% and a comment% like this one - its doable though

An 
$ inline \ math $
purposely placed on a separate line
should remain as it is.

% comment line.
\end{document}

The output file:

\documentclass{article}
\usepackage{amsmath}

 \newcommand\multiline{      It will mess up multiline commands, better don't try!!!      } 

\begin{document}

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac,  $\int$ adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. 


Two empty lines above test. 

A line with a comment % comment
should remain as it is but it will trigger on percent signs, 
like 100\%,
as well because there might be lines with both 100\% and a comment% like this one - its doable though

An 
$ inline \ math $
purposely placed on a separate line should remain as it is. 

% comment line.
\end{document}
4
  • What does it do with code that has an equation within a paragraph or with an already nicely indented itemize or something?
    – cgnieder
    Jul 5, 2016 at 12:20
  • Willing to file a bug?:) As said, this is for sloppy text (like when extracting from pdf), not for nicely written code. It's a simple code you can apply to parts of your source file (not necessarily tex), more for literature than scientific docs (where word-wrapping doesn't make too much sense anyway). @clemens
    – alwaysask
    Jul 5, 2016 at 12:36
  • Ok. I was just curious :)
    – cgnieder
    Jul 5, 2016 at 12:44
  • That's why I included the \multiline example in the test file, to show it won't work. It's not intended to be an interpreter. It can be attached to a shortcut, select the bit of text to be converted in your [GUI] editor, and convert it on the spot. @clemens
    – alwaysask
    Jul 5, 2016 at 13:11

You must log in to answer this question.

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