1

我有以下文件夹结构

   1st-grade-math-class/
      common/
        mystyle.sty
        mysubstyle.sty
        fonts/
          font1.ttf
          font2.ttf
          font3.ttf
      week01/
        handout.tex
        image1.pdf
        image2.pdf
      week02/
        handout.tex
        image1.pdf
        image2.pdf
      ...
      week13/
        handout.tex
      output/
        [empty]

我想创建一个 Makefile - 以最好的方式 - 执行以下操作:

  • 确保我在 TEXINPUTS 中正确包含公共目录

  • 将handout.tex编译为 PDF(使用 pdflatex 或 xelatex)并将其作为week01-handout-student.pdf 放在输出目录中

  • 在文件开头(设置标志)前面加上一行 LaTeX将handout.tex编译为 PDF,并将其作为week01-handout-teacher.pdf 放在输出目录中

  • 清理所有内容(日志、辅助等文件)

除了在每个子目录中手动复制一个基本的 Makefile/bash 脚本,然后使用 for 循环一个一个地调用它们之外,我不确定我是否知道如何以任何其他方式执行此操作。

我将不胜感激有关如何构建此过程的帮助,最好是在根目录中使用单个 Makefile。谢谢。

更新:我故意不想提供任何关于我如何编译 LaTeX 的细节,以防有人有比我目前使用的更好的建议。现在我正在使用 Latexmk(它已经是一个类似于 make 的 LaTeX 包装器):

  • latexmk -pdf file.tex生成file.pdf
  • 添加代码行,我做一个简单的回显“代码行”> temp.tex和cat handout.tex >> temp.tex,然后是相同的latexmk命令
  • 目录中的latexmk -c file.tex清除所有用于编译file.tex 的临时文件
  • TEXINPUTS 是 TeX 路径变量,让 TeX 找到(在其路径中)样式文件:我执行 TEXINPUTS=full-path-to/common 然后在编译任何东西之前导出 TEXINPUTS。

如果有人有更好的建议,我愿意接受。

4

2 回答 2

1

我相信这样的事情应该做你想做的事:

OUTDIR := output

# Tell make to export this environment variable to all recipe lines it runs.
export TEXINPUTS := $(abspath common)

# Get the list of all of our week directories.
weekdirs := $(wildcard week*)
#$(info weekdirs:$(weekdirs))

# Create student output filenames from week directory names.
STUDENT_HANDOUTS := $(patsubst %,$(OUTDIR)/%-handout-student.pdf,$(weekdirs))
#$(info STUDENT_HANDOUTS:$(STUDENT_HANDOUTS))

# Create teacher output filenames from week directory names.
TEACHER_HANDOUTS := $(patsubst %,$(OUTDIR)/%-handout-teacher.pdf,$(weekdirs))
#$(info TEACHER_HANDOUTS:$(TEACHER_HANDOUTS))

# Default target depends on all output files.
all: $(STUDENT_HANDOUTS) $(TEACHER_HANDOUTS)

# Pattern rule for building pdf files.
%.pdf:
        @echo + Making $@ from $^
        @echo cd $(@D) && echo latexmx -pdf $(abspath $<)
        @echo cd $(@D) && echo latexmk -c $(abspath $<)

# Static pattern rule mapping student output files to input files.
$(STUDENT_HANDOUTS) : $(OUTDIR)/%-handout-student.pdf : %/handout.tex

# Pattern rule to generate temporary input files from original input files.
%/handout-tmp.tex: %/handout.tex
        @echo echo 'line of code' '>' $@
        @echo cat $^ '>>' $@

# Static pattern rule mapping teacher output files to (temporary) input files.
$(TEACHER_HANDOUTS) : $(OUTDIR)/%-handout-teacher.pdf : %/handout-tmp.tex

取消注释这些$(info)行以了解变量是如何组合在一起的。

这用于latexmk -c在创建输出文件后清理辅助文件。

于 2014-10-01T16:19:40.010 回答
1

技术 1echo foo >$@; cat $< >>$@事情是我以前做过的事情;我认为这是合理的。

技术2。另一种技术是让您的.tex文档或它使用的包文件包含如下行:

\InputIfFileExists{lecturenotes.config}{}{}

这允许您在 makefile 中进行某些调整,例如:

# any bits of configuration that should be in all the .config files
SWITCHES=\makeindex

%-notes.pdf: %.tex
        printf '$(SWITCHES)\\ExecuteOptions{sidenotes}\n' >lecturenotes.config
        TEXINPUTS=styles: pdflatex $<
        mv ${<:.tex=.pdf} $@    
        rm lecturenotes.config

%-single.pdf: %.tex
        printf '$(SWITCHES)\\ExecuteOptions{oneside}\n' >lecturenotes.config
        TEXINPUTS=styles: pdflatex $<
        mv ${<:.tex=.pdf} $@
        rm lecturenotes.config

技术 3。从外部控制 LaTeX 的第三种技术是包含在您的文档(或包文件)中:

\newif\ifdemonstrator
\expandafter\ifx\csname demonstrator\endcsname\relax
  \demonstratorfalse
\else
  \demonstratortrue
\fi
...
\ifdemonstrator
    \typeout{Demonstrator is TRUE}
\else
    \typeout{Demonstrator is FALSE}
\fi

然后调用乳胶:

%-plain.pdf: %.tex
    latex $<
    mv ${<:.tex=.pdf} $@

%-demo.pdf: %.tex
    latex --jobname ${<:.tex=} '\def\demonstrator{x}\input{$<}`
    mv ${<:.tex=.pdf} $@

技巧 1 有点生硬,但如果只需要这些,它就相当轻巧。

技术 2 可能是最巧妙的设计,但它稍微费力一些。

技术 3 可能是我在这种情况下使用最多的一种。

于 2014-10-01T20:20:45.320 回答