在 Linux 中,我创建了一个 Makefile,根据项目所需的模式重命名输出的 pdf 文件。这些模式由从 tex 源文件本身获取的变量填充。
前任 :
project.sty(我在这里为一个项目的所有文档定义大多数通用变量)
\def\AfsnitNavn{Tagpaneler}
\def\AfsnitNr{40}
\def\ProjektNavn{En by i Sverige}
\def\ProjektNr{120}
...
etc
projektredgørelse.tex(我需要编译的文档之一)
\documentclass{paneltrade}
\def\doctype{Statisk projektredegørelse}
\def\hierar{B1.2}
\title{\dokTitle}
\date{\today}
\author{\ingenior}
\begin{document}
\maketitle
\section*{Konstruktionsafsnit}
\AfsnitNavn{}\\
...
\end{document}
Makefile
TEX=latexmk
FLAGS=-lualatex -interaction=nonstopmode -halt-on-error
# This is where are my main variables
TEX_FILE = project.sty
%.pdf: %.tex
$(eval AUTHOR = $(shell grep -oP '\\def\\ingenior{\K[^}]+' $(TEX_FILE)))
$(eval AFSNIT = $(shell grep -oP '\\def\\AfsnitNavn{\K[^}]+' $(TEX_FILE)))
$(eval NR = $(shell grep -oP '\\def\\AfsnitNr{\K[^}]+' $(TEX_FILE)))
$(eval PROJECT = $(shell grep -oP '\\def\\ProjektNavn{\K[^}]+' $(TEX_FILE)))
$(eval TYPE = $(shell grep -oP '\\def\\doctype{\K[^}]+' $<))
$(eval HIERAR = $(shell grep -oP '\\def\\hierar{\K[^}]+' $<))
$(TEX) $(FLAGS) $<
mv $@ "$(PROJECT) $(HIERAR).$(NR) $(TYPE) - $(AFSNIT).pdf"
kk1: konstruktionsgrundlag.pdf Statiskeberegninger.pdf projektredgørelse.pdf
如何在 Windows 下的 vscode/vscodium 中重现该系统(我已安装 vscodium,并安装了 Git、Latex workshop 和 Latex 实用程序)?有没有人描述过我可以使用类似的系统?
如果我的系统需要一点重组,我可以接受(从 json 或类似的东西中填充标题或变量,我可以,但我不知道如何去做......)。
感谢您的帮助和耐心。
答案1
我确实做到了。
# Define LaTeX compiler and flags
$TEX = "latexmk"
$FLAGS = "-lualatex"
# Define the name of the LaTeX file
$TEX_FILE = "project.sty"
# Function to compile a LaTeX file to PDF
function Compile-LatexFile {
param (
[string]$TexFile
)
# Extract necessary information from the TEX_FILE
$AUTHOR = (Get-Content $TEX_FILE | Select-String -Pattern '\\def\\ingenior{([^}]*)}' | ForEach-Object { $_.Matches.Groups[1].Value })
$AFSNIT = (Get-Content $TEX_FILE | Select-String -Pattern '\\def\\AfsnitNavn{([^}]*)}' | ForEach-Object { $_.Matches.Groups[1].Value })
$NR = (Get-Content $TEX_FILE | Select-String -Pattern '\\def\\AfsnitNr{([^}]*)}' | ForEach-Object { $_.Matches.Groups[1].Value })
$PROJECT = (Get-Content $TEX_FILE | Select-String -Pattern '\\def\\ProjektNavn{([^}]*)}' | ForEach-Object { $_.Matches.Groups[1].Value })
$TYPE = (Get-Content $TexFile | Select-String -Pattern '\\def\\doctype{([^}]*)}' | ForEach-Object { $_.Matches.Groups[1].Value })
$HIERAR = (Get-Content $TexFile | Select-String -Pattern '\\def\\hierar{([^}]*)}' | ForEach-Object { $_.Matches.Groups[1].Value })
if ($REV -gt 0) {
$PDF = "$PROJECT $HIERAR.$NR $TYPE - $AFSNIT - Rev $REV.pdf"
} else {
$PDF = "$PROJECT $HIERAR.$NR $TYPE - $AFSNIT.pdf"
}
# Compile LaTeX file if PDF file does not exist or is older than TEX file
if (-not (Test-Path $PDF) -or (Get-Item $TexFile).LastWriteTime -gt (Get-Item $PDF).LastWriteTime) {
Write-Output "Compiling $TexFile to $PDF"
& $TEX $FLAGS $TexFile
# Rename output to desired PDF name
Move-Item "$($TexFile.Replace('.tex', '.pdf'))" $PDF -Force
}
}
# Compile all necessary PDFs for kk1 target
function Compile-Kk1 {
Compile-LatexFile "konstruktionsgrundlag.tex"
Compile-LatexFile "statiskeberegninger.tex"
Compile-LatexFile "projektredgoerelse.tex"
}
# Compile all necessary PDFs for kk2 target
function Compile-Kk2 {
Compile-Kk1
Compile-LatexFile "Statiskkontrolplanprojektering.tex"
Compile-LatexFile "kontrolrapport.tex"
}
# Compile brev.tex to PDF
function Compile-Brev {
& $TEX $FLAGS "brev.tex"
}
# Clean up auxiliary files
function Clean {
& $TEX -C
}
# Clean up all PDF files
function Clean-PDF {
Remove-Item "*.pdf" -Force
}
# Main targets
function Main {
param (
[string]$Target
)
switch ($Target) {
"kk1" { Compile-Kk1 }
"kk2" { Compile-Kk2 }
"brev" { Compile-Brev }
"clean" { Clean }
"cleanpdf" { Clean-PDF }
"cleanall" { Clean; Clean-PDF }
default { Compile-LatexFile $Target }
}
}
# Parse command-line arguments to determine the target
if ($args.Length -eq 0) {
Write-Output "Usage: .\LaTeXBuild.ps1 <target>"
Write-Output "Targets: kk1, kk2, brev, clean, cleanpdf, cleanall"
} else {
Main -Target $args[0]
}