我正在尝试使用 latex 和 filecontents 或 fancyvrb 包生成外部 Makefile(和其他文件)。由于\t
输入文件中的(制表符)字符在输出文件中转换为空格(请参阅将制表符输出到文件中,我决定使用一点 Lua 来做一个小技巧(我知道你可以用sed
或perl
)。
由于我不太熟悉,Lua
但写了以下代码(论坛将真实的制表符转换为空格example.tex
):
% example.tex
% arara: lualatex : {draft: yes,action: nonstopmode}
% arara: pdflatex
% arara: clean: { files:[example.aux,example.log,Makefile.tmp,Makefile.tab] }
\documentclass{article}
\usepackage{ifluatex}
\usepackage{filecontents}
\usepackage{xcolor}
\usepackage{fvextra}
\begin{document}
The original Makefile whit real tabs (show whit arrow)
%% forum convert tabs into space...replace whit real tab (show in red)
\begin{Verbatim}[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red,showspaces, spacecolor=blue]
clean: clean-test
rm -f *.bbl *.bcf *.bib *.blg *.xdy # biblatex
distclean: clean
rm -f *.cls *.sty *.clo *.tar.gz *.tds.zip README
%.cls: %.dtx
tex $<
%.pdf: %.dtx
pdflatex -interaction=nonstopmode -halt-on-error $<
\end{Verbatim}
Write in external file (Makefile.tab)
\begin{VerbatimOut}{Makefile.tab}
clean: clean-test
rm -f *.bbl *.bcf *.bib *.blg *.xdy # biblatex
distclean: clean
rm -f *.cls *.sty *.clo *.tar.gz *.tds.zip README
%.cls: %.dtx
tex $<
%.pdf: %.dtx
pdflatex -interaction=nonstopmode -halt-on-error $<
\end{VerbatimOut}
Read Makefile.tmp file, (tabs are convert in space)
\VerbatimInput[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red,showspaces, spacecolor=blue]{Makefile.tab}
%%
\ifluatex
Create lua funtion only in lualatex
\begin{filecontents*}{maketab.lua}
-- maketab
local open = io.open
local function read_file(filename)
local file = open(filename, "r") -- r read mode
if not file then return nil end
local slurp = file:read "*all" -- reads the whole file (slurp)
file:close()
return slurp
end
local content = read_file("Makefile.tmp");
content=content:gsub("TAB ", "\t")
-- Create new Makefile.mk (pdfTeX need ext)
outwhitmk = io.open("Makefile.mk", "w")
outwhitmk:write(content)
outwhitmk:close()
-- Create new Makefile (no ext)
outreal = io.open("Makefile", "w")
outreal:write(content)
outreal:close()
\end{filecontents*}
\else
\fi
Write a external Makefile.tmp changue tab for TAB \newline
\begin{filecontents*}{Makefile.tmp}
clean: clean-test
TAB rm -f *.bbl *.bcf *.bib *.blg *.xdy # biblatex
distclean: clean
TAB rm -f *.cls *.sty *.clo *.tar.gz *.tds.zip README
%.cls: %.dtx
TAB tex $<
%.pdf: %.dtx
TAB pdflatex -interaction=nonstopmode -halt-on-error $<
\end{filecontents*}
Show external Makefile.tmp \newline
\VerbatimInput[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red,showspaces, spacecolor=blue]{Makefile.tmp}
Now using directlua maketab.lua to reverse TAB to real tab character\newline
\ifluatex
\directlua{dofile("maketab.lua")}
Show correct MakeFile (LuaTeX don't need ext)
\VerbatimInput[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red,showspaces, spacecolor=blue]{{Makefile}}
\else
Show correct MakeFile (pdfTeX)
\VerbatimInput[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red,showspaces, spacecolor=blue]{Makefile.mk}
\fi
\end{document}
我对此有两点疑问:
从 LaTeX 创建 Makefile 文件是否正确,或者是否有任何包可以执行此操作(我不想重新发明轮子)。
参照 Lua 代码(
maketab.lua
),可以直接按如下方式重写代码\directlua{dofile("maketab.lua",filein.ext,fileout[.ext]?)}
?,传递带扩展名的输入文件的名称和带可选扩展名的输出文件的名称。
提前表示感谢。
答案1
我自己回答,您可以使用该scontents
包,而不需要编写lua
脚本来执行此操作:
\documentclass{article}
\usepackage{scontents}
\usepackage{xcolor}
\usepackage{fvextra}
\begin{document}
The original Makefile whit real tabs (show red arrow)
\begin{Verbatim}[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red]
clean: clean-test
rm -f *.bbl *.bcf *.bib *.blg *.xdy
distclean: clean
rm -f *.cls *.sty *.clo *.tar.gz *.tds.zip README
\end{Verbatim}
Write in external file (Makefile.tab)
\begin{scontents}[write-out=Makefile.tab]
clean: clean-test
rm -f *.bbl *.bcf *.bib *.blg *.xdy
distclean: clean
rm -f *.cls *.sty *.clo *.tar.gz *.tds.zip README
\end{scontents}
Read Makefile.tab file (show red arrow)
\VerbatimInput[obeytabs, showtabs, tab=\rightarrowfill, tabcolor=red]{Makefile.tab}
\end{document}