You can set up simple templating system with lua.
Sample template sample.tpl
:
---
title = "Sample document",
extra_packages = "cmap",
encoding="utf8",
class = "article"
---
\documentclass{${class}}
\usepackage[${encoding}]{inputenc}
\usepackage{${extra_packages}}
\title{${title}}
\begin{document}
\maketitle
${content}
\end{document}
In header delimited with ---
strings, default variables are set. They are then included in the code with ${variable_name}
. You can overide the variables in the document:
---
title = "Hello world",
extra_packages="lipsum"
---
\section{First one}
\lipsum[1-4]
Now the script luatextemplating.lua
kpse.set_program_name("luatex")
-- Helper functions
-- interp: simple templating
-- usage: print( "${name} is ${value}" % {name = "foo", value = "bar"} )
function interp(s, tab)
return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
getmetatable("").__mod = interp
-- Parse page variables
function table.unserialize(s)
local getTableFromString = loadstring('return '..s)
local t = assert(getTableFromString())
if type(t) ~= 'table' then return end
return t
end
function string:split(pat)
pat = pat or '%s+'
local st, g = 1, self:gmatch("()("..pat..")")
local function getter(segs, seps, sep, cap1, ...)
st = sep and seps + #sep
return self:sub(segs, (seps or 0) - 1), cap1 or sep, ...
end
return function() if st then return getter(st, g()) end end
end
--
function parseInputFile(filename)
local f=io.open(filename)
local s = f:read("*a")
f:close()
return parseInput(s)
end
function parseInput(s)
local start = s:find("---") + 3
local header_end= s:find("---",start)
local header= s:sub(start,header_end-1)
local content= s:sub(header_end+3)
return table.unserialize("{"..header.."}"),content
end
-- Execution
local arg_message=[[
luatextemplating - generate TeX files from simple templates
usage:
luatextemplating templatefile inputfile
result is printed to the stdout
]]
if #arg < 2 then
texio.write_nl(arg_message)
return 0
end
local main_header, template = parseInputFile(arg[1])
local file_header, content= parseInputFile(arg[2])
for k, v in pairs(file_header) do
main_header[k]=v
end
main_header["content"]=content
print(template % main_header)
After running texlua luatextemplating.lua path/to/templates/sample.tpl sample.tex
you get this document at the standard output
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{lipsum}
\title{Hello world}
\begin{document}
\maketitle
\section{First one}
\lipsum[1-4]
\end{document}
.sty
files in a specific directory and then include them in my.tex
file via a relative path. This makes it very clear in the the source which ones are my custom versions..sty
as a template? I guess these are two different things. Maybe, due to my poor understanding, I referred to class files, while they are also something else..sty
files I do as above, and for.tex
files which are mostly blank but are reusable for a particular setup that I use often, I store them in the specific place depending on the IDE I use -- as per @R.Schumacher answer below. ForTeXShop
I store them in~/Library/TeXShop/Stationery/
and then I can access the file fromFile/New from Stationary
, and forTeXWorks
I put them in~/Library/TeXworks/templates/
and access it viaFile/New from Template
.