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.

  • 1
    Why would you want to write a Makefile using LaTeX? filecontents should be used before \documentclass. – cfr Feb 19 '17 at 4:41
  • The truth is I'm trying to document a scrip (perl / c ++) in latex and then pass them DTX. And the` \ t` characters are important for the files generated (not just Makefile, Readme.md, source.c, etc ...) – Pablo González L Feb 19 '17 at 4:57
  • Edit the question and add some sample code to clarify the idea ... the forum converts ` \ t`(real tab) to spaces. – Pablo González L Feb 19 '17 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.