1

I'm trying to generate a external Makefile (and others) file using latex and the filecontents or fancyvrb package. As the \t (tabs) characters in input file are converted to space in output file (see output tabulation into a file, i have decided to do a little trick using a little Lua (i know you can with sed or perl).

As I am not very familiar with Lua but written the following code (The forum convert real tabs to spaceexample.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}

and I have two doubts about it:

  1. Is the correct way to create a Makefile file from LaTeX, or is there any package that can do this (I do not want to re-invent the wheel).

  2. Referring to the Lua code(maketab.lua), is possible to rewrite the code in the following direct \directlua{dofile("maketab.lua",filein.ext,fileout[.ext]?)}? , Pass the name of the input file with extension and the name of the output file with optional extension.

Grateful in advance.

3

1 Answer 1

1

I answer myself, you can use the scontents package, and you don't need to write a lua script to do it:

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

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .