I’m using pandoc (with the pandoc-crossref filter) to convert markdown documents to LATEX. But I want tables to be outputted as regular (floating) table environments rather than as (non-floating) longtables. I’ve written a LUA filter to do this, which is successful, except that it fails to extract the information about the table’s pandoc-crossref label. I need to extract this information so that I can include this label as part of the LATEX output (and thereby crossreference the table). Here’s a minimal example.
Firstly, here’s a minimal markdown document (input.md) containing just a table with the label tbl:table1 (which can be read by pandoc-crossref):
| First Header | Second Header |
|:-------------|:--------------|
| Content Cell | Content Cell |
| Content Cell | Content Cell |
: Table example {#tbl:table1}
Secondly, here’s my minimal LUA-filter (table.lua) that attempts to extract this label (tbl:table1) from this markdown document:
function Table(elem)
local id = elem.attr.identifier or ''
if id ~= '' then
print("Table label: " .. id)
else
print("Table does not have a label")
end
end
return {
{ Table = Table }
}
Thirdly, when I run pandoc from the command prompt, I expect this LUAfilter to print “Table label: tbl:table1” in the command prompt window, but instead it prints “Table does not have a label”. Here’s my command line in the command prompt:
pandoc input.md --output=output.tex --from markdown+pipe_tables --to latex --filter pandoc-crossref --lua-filter=table.lua
Any help greatly appreciated!
By the way, here is my wider LUA filter that (almost successfully) outputs a table rather than a longtable:
function Table(tbl)
local simpleTable = pandoc.utils.to_simple_table(tbl)
local blocks = pandoc.Blocks{}
-- Function to create a LaTeX row
local function create_latex_row(cells)
local latex_row = ""
for i, cell in ipairs(cells) do
for _, content in ipairs(cell) do
latex_row = latex_row .. pandoc.write(pandoc.Pandoc({content}), "latex")
end
if i < #cells then
latex_row = latex_row .. " & "
end
end
return latex_row .. " \\"
end
-- Determine the number of columns (using header or first row)
local num_cols = 0
if simpleTable.header and #simpleTable.header > 0 then
num_cols = #simpleTable.header
elseif #simpleTable.rows > 0 then
num_cols = #simpleTable.rows[1]
end
-- Assume all columns are left-aligned for simplicity
local col_alignment = string.rep("l", num_cols)
-- Begin table environment
blocks:insert(pandoc.RawBlock("latex", "\begin{table}[tbp]"))
-- Add caption
if tbl.caption then
local caption_text = pandoc.utils.stringify(tbl.caption)
blocks:insert(pandoc.RawBlock("latex", "\caption{" .. caption_text .. "}"))
end
-- Add label using tbl.attr.identifier
-- This is the part that doesn't work
if tbl.attr and tbl.attr.identifier and tbl.attr.identifier ~= "" then
blocks:insert(pandoc.RawBlock("latex", "\label{" .. tbl.attr.identifier .. "}"))
end
blocks:insert(pandoc.RawBlock("latex", "\centering"))
blocks:insert(pandoc.RawBlock("latex", "\begin{tabular}{" .. col_alignment .. "}"))
-- Process header
if simpleTable.header and #simpleTable.header > 0 then
local header_row = create_latex_row(simpleTable.header)
blocks:insert(pandoc.RawBlock("latex", header_row))
end
-- Process rows
for _, row in ipairs(simpleTable.rows) do
local latex_row = create_latex_row(row)
blocks:insert(pandoc.RawBlock("latex", latex_row))
end
-- End tabular and table environment
blocks:insert(pandoc.RawBlock("latex", "\end{tabular}"))
blocks:insert(pandoc.RawBlock("latex", "\end{table}"))
return blocks
end
bearofthesea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.