LUAfilter for pandoc (with crossref) that extracts a table’s label

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

New contributor

bearofthesea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật