Warning: this is an htmlized version! The original is across this link, and the conversion rules are here.
```-- treetex.lua: derivation trees and functions to convert them to TeX.
-- This file:
--   http://angg.twu.net/dednat5/treetex.lua.html
--   http://angg.twu.net/dednat5/treetex.lua
--                    (find-dn5 "treetex.lua")
-- Author: Eduardo Ochs <eduardoochs@gmail.com>
-- Version: 2011apr04
--

-- intersecting   (find-dn4 "dednat4.lua" "tree-lib")
-- tatsuta        (find-dn4 "dednat4.lua" "tree-out")
-- paultaylor

require "eoo"       -- (find-dn5 "eoo.lua")

TreeNode = Class {
type    = "TreeNode",
__index = {
hasbar    = function (tn) return tn.bar ~= nil end,
barchar   = function (tn) return tn.bar end,
TeX_root  = function (tn) return tn[0] end,
TeX_label = function (tn) return tn.label end,
nhyps     = function (tn) return #tn end,
hypslist  = function (tn) return tn end,
},
}

TeX_subtree_tatsuta = function (tn, i_)
if not tn:hasbar() then
return i_.."\\mathstrut "..tn:TeX_root()
else
local r_ = tn:TeX_root()
local b_ = tn:barchar()
local l_ = tn:TeX_label()
local h_ = tn:hypslist()
local r  = "\\mathstrut "..r_
local b  = ({["-"]="", ["="]="=", [":"]="*"})[b_]
local l  = (l_ and "[{"..l_.."}]") or ""
local i  = i_.." "
local f  = function (tn) return TeX_subtree_tatsuta(tn, i) end
local h  = mapconcat(f, h_, " &\n")
return i_.."\\infer"..b..l.."{ "..r.." }{\n"..h.." }"
end
end
TeX_deftree_tatsuta = function (tn, name, link)
TeX_subtree_tatsuta(tn, " ").." }"
end

TreeNode.__index.TeX_subtree = TeX_subtree_tatsuta
TreeNode.__index.TeX_deftree = TeX_deftree_tatsuta

-- dump-to: tests
--[==[
* (eepitch-lua51)
* (eepitch-kill)
* (eepitch-lua51)
require "treetex"
TN = function (root, b, l, ...)
return TreeNode {[0]=root, bar=b, label=l, ...}
end
T = TN("f(1+2+3)", "-", "app",
TN("f"),
TN("1+2+3", "=", nil,
TN"1",
TN"2",
TN"3"))
print(T:TeX_subtree("  "))
print(T:TeX_deftree("f(1+2+3)"))

-- Here's a typical TreeNode structure:
PP(T)
-- {0="f(1+2+3)",
--    1={0="f"},
--    2={0="1+2+3",
--         1={0="1"},
--         2={0="2"},
--         3={0="3"},
--       "bar"="="},
--  "bar"="-",
--  "label"="app"}

--]==]

-- Local Variables:
-- coding:             raw-text-unix
-- ee-anchor-format:   "«%s»"
-- End:
```