从编辑器中对齐 TeX 代码

从编辑器中对齐 TeX 代码

也许作者(或编辑)应该有能力做到这一点。但无论如何,我想知道我是否能用 TeX 做到这一点。

我通常希望我的 TeX 代码看起来合理一些。有什么办法吗?

我正是想避免混乱:

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. 

并想找到一种方法,让我的代码自动地看起来或多或少是合理的(在我写完大部分内容之后):

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 

我正在使用 Kile(但我不介意使用另一个,只需在那里打开代码,然后在 Kile 中工作)。

答案1

我安装了emacs.tex在那里打开了我的文档,并M-q按照注释中的建议使用了它。原则上,它是有效的。但M-q不关心%注释行,因此这种方法不是 100% 安全的。

否则,安装 AUCTeX,它就可以工作。

答案2

我编写了一个 Python 脚本,用于将杂乱的输入文本(如上面的“混乱”示例)转换为“每行一个段落”的文本,该文本几乎可以通过任何编辑器对齐(自动换行)显示。当文本是从 PDF 文件中提取或从其他软件导入时也很有用。

我希望它尽可能简单,让任何不懂 Python 的人都能轻松理解/修改(比如我:))。

笔记:请不要在序言或其他 TeX 命令块上使用。仅提取单独文件中的相关文本。请备份你的工作在使用脚本之前!!

Python 脚本(未在 Windows 主机或文本上测试):

#
# 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

考试输入文件基于上面的“混乱”示例:

\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}

输出文件

\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}

相关内容