分类
LaTeX

使用LaTeXindent自动格式化你的tex文档

在修改文章时很容易把格式搞乱, 这就需要重新格式化我们的文档。在这里谈一下Windows下的方法, 即使用latexindent.exe来格式化你的文档。

  1. 首先, 我们需要更新下ctex自带的latexindent.exe, 否则老是提醒我们:The Perl interpreter could not be found. 为此, 只需要到ctan下载la­texin­dent.exe, 并搜索你ctex安装目录下的旧latexindent.exe, 然后覆盖掉.
  2. 然后, 定位到你要格式化的tex文件, 我们这里假设是D:\test.tex, 运行
    latexindent -w test.tex
    这样你重新打开test.tex时就发现他已经重新格式化了。
  3. 为了得到较好的效果, 一般来说环境都要放到单独的一行。
  4. 最后, 还可以用自己的配置文件来设置格式。为此, 在上面的ctan下载de­fault­Set­tings.yaml到你test.tex同目录, 然后复制一份到localSettings.yaml, 我把\t都替换为两个空格, 并保存。然后只需在你前面的格式化命令中添加-l开关即可。 即
    latexindent -w -l test.tex
  5. 最后, 我们还可以为WinEdt添加一个LaTeX-Indent菜单。为此, 只需要打开options->options interface->Menus and Toolbar->Main Menu, 然后在最后一个END之前添加如下代码, 保存并按F9加载即可看到菜单:
    MENU="&LaTeX-Indent"
     ITEM="LaTeX-Indent"
         CAPTION="latexindent"
         IMAGE="TeXIconTeX"
         SAVE_INPUT=1
         MACRO=`WinExe('','latexindent -w -l "%F"','%P','LaTeX-Indent...',100,0,'','','',11);`
         SHORTCUT="24649::Shift+Ctrl+I"
         REQ_FILTER=:"%!M=TeX"|"%!M=TeX:STY"|"%!M=TeX:AUX" 
    

附上我修改后的yaml文件:

# 
# defaultSettings.yaml
#
# You're welcome to change anything you like in here, but 
# it would probably be better to have your own user settings 
# files somewhere else- remember that this file may be overwritten
# anytime that you update your distribution. Please see the manual
# for details of how to setup your own settings files.
#
# Please read the manual first to understand what each switch does :)

# Default value of indentation
defaultIndent: " "

# default file extension of backup file (if original is overwritten with -w switch)
# for example, if your .tex file is called
#       myfile.tex
# and you specify the backupExtension as BACKUP.bak then your 
# backup file will be
#       myfileBACKUP.bak
backupExtension: .bak

# only one backup per file; if onlyOneBackUp is 0 then the
# number on the extension increments by 1 each time
# (this is in place as a safety measure) myfile.bak0, myfile.bak1, myfile.bak2
#
# if you set onlyOnebackUp to 1, then the backup file will
# be overwritten each time (not recommended until you trust the script)
onlyOneBackUp: 0

# some users may only want a set number of backup files, 
# say at most 3; in which case, they can change this switch.
# If maxNumberOfBackUps is set to 0 (or less) then infinitely 
# many backups are possible, unless onlyOneBackUp is switched on
maxNumberOfBackUps: 0

# some users may wish to cycle through back up files, for example, 
# with maxNumberOfBackUps: 4, they may wish to delete the oldest
# back up file, and keep only the most recent.
#
#    copy myfile.bak1 to myfile.bak0
#    copy myfile.bak2 to myfile.bak1
#    copy myfile.bak3 to myfile.bak2
#    copy myfile.bak4 to myfile.bak3
#
# the back up will be written to myfile.bak4
cycleThroughBackUps: 0

# indent preamble
indentPreamble: 0

# always look for split { }, which means that the user doesn't
# have to complete checkunmatched, checkunmatchedELSE
alwaysLookforSplitBraces: 1

# always look for split [ ], which means that the user doesn't
# have to complete checkunmatchedbracket
alwaysLookforSplitBrackets: 1

# remove trailing whitespace from all lines 
removeTrailingWhitespace: 1

# environments that have tab delimiters, add more 
# as needed
lookForAlignDelims:
   tabular: 1
   tabularx: 1
   longtable: 1
   array: 1
   matrix: 1
   bmatrix: 1
   pmatrix: 1
   align: 1
   align*: 1
   alignat: 1
   alignat*: 1
   aligned: 1
   cases: 1
   dcases: 1
   listabla: 1

# if you have indent rules for particular environments
# or commands, put them in here; for example, you might just want 
# to use a space " " or maybe a double tab "  "
indentRules:
   myenvironment: "  "
   anotherenvironment: "  "
   chapter: " "
   section: " "
   item: "  "

#  verbatim environments- environments specified 
#  in this hash table will not be changed at all!
verbatimEnvironments:
    verbatim: 1
    lstlisting: 1

#  no indent blocks (not necessarily verbatim 
#  environments) which are marked as %\begin{noindent}
#  or anything else that the user puts in this hash
#  table
noIndentBlock:
    noindent: 1
    cmhtest: 1

# if you don't want to have additional indentation 
# in an environment put it in this hash table; note that
# environments in this hash table will inherit 
# the *current* level of indentation they just won't
# get any *additional*.
noAdditionalIndent:
    myexample: 1
    mydefinition: 1
    problem: 1
    exercises: 1
    mysolution: 1
    foreach: 0
    widepage: 1
    comment: 1
    \[: 0
    \]: 0
    document: 1
    frame: 0

# if you want to add indentation after
# a heading, such as \part, \chapter, etc
# then populate it in here - you can add 
# an indent rule to indentRules if you would 
# like something other than defaultIndent
#
# you can also change the level if you like, 
# or add your own title command
indentAfterHeadings:
    part:
       indent: 0
       level: 1
    chapter: 
       indent: 0
       level: 2
    section:
       indent: 0
       level: 3
    subsection:
       indent: 0
       level: 4
    subsection*:
       indent: 0
       level: 4
    subsubsection:
       indent: 0
       level: 5
    paragraph:
       indent: 0
       level: 6
    subparagraph:
       indent: 0
       level: 7

# if you want the script to look for 
  • commands # and format it, as follows (for example), #
      #
    • content here # next line is indented # next line is indented #
    • another item #
    # then populate indentAfterItems. See also itemNames indentAfterItems: itemize: 1 enumerate: 1 list: 1 # if you want to use other names for your items (such as, for example, part) # then populate them here- note that you can trick latexindent.pl # into indenting all kinds of commands (within environments specified in # indentAfterItems) using this technique. itemNames: item: 1 myitem: 1 # if you want to indent if, else, fi constructs such as, for example, # # \ifnum#1=2 # something # \else # something else # \fi # # then populate them in constructIfElseFi constructIfElseFi: ifnum: 1 ifdim: 1 ifodd: 1 ifvmode: 1 ifhmode: 1 ifmmode: 1 ifinner: 1 if: 1 ifcat: 1 ifx: 1 ifvoid: 1 ifeof: 1 iftrue: 1 ifcase: 1 # *** NOTE *** # If you have specified alwaysLookforSplitBraces: 1 # and alwaysLookforSplitBrackets: 1 then you don't need # to worry about completing # # checkunmatched # checkunmatchedELSE # checkunmatchedbracket # # in other words, you don't really need to edit anything # below this line- it used to be necessary for older # versions of the script, but not anymore :) #*** *** # commands that might split {} across lines # such as \parbox, \marginpar, etc checkunmatched: parbox: 1 vbox: 1 # very similar to %checkunmatched except these # commands might have an else construct checkunmatchedELSE: pgfkeysifdefined: 1 DTLforeach: 1 ifthenelse: 1 # commands that might split [] across lines # such as \pgfplotstablecreatecol, etc checkunmatchedbracket: pgfplotstablecreatecol: 1 pgfplotstablesave: 1 pgfplotstabletypeset: 1 mycommand: 1 psSolid: 1
  • 发表回复

    您的电子邮箱地址不会被公开。 必填项已用*标注

    此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据