1

In HTML, my table has extra cells below the header row and below the left hand cell on the bottom row.

\begin{longtable}{ | p{2.0cm} | *{5}{>{\raggedright\arraybackslash}p{3.5cm} |} } 
\hline
\b{Columnhead} & \b{Columnhead} & \b{Columnhead} & \b{Columnhead} &
\b{Columnhead}\\ 
\hline \endhead

Cell &

Cell \newline
Line \newline
Line \newline
Line - line n &

Cell &

Cell &

Cell \\

\hline

Cell &

Cell \newline
Line \newline
Line \newline
Line - line n &

Cell &

Cell &

Cell \\

\hline
\end{longtable}

misplaced cells

  • The PDF output displays well. I dont understand these tables enough to troubleshoot the problem. It seems that the first value in the header row definition (p{2.0cm}) ensures that there is a right hand margin. and the other value in that line defines the width fo the columns (p{3.5cm}). – Robert Werner Oct 16 '19 at 16:09
  • 1
    By "in HTML", do you mean "when processed with tex4ht"? If so, there is a known bug in connection with longtable: puszcza.gnu.org.ua/bugs/?196 which you may be hitting. Perhaps one of the developers will weigh in. – Alex Watson Oct 16 '19 at 16:15
  • @AlexWatson Yes it turns out that tex4ht is used. Any ideas on a better way to format my tables? I had for requirements for my table formatting: allow headers for long tables to carry over pages in PDF output, left align content with ragged edges and have multiple lines in table cells, grid lines. – Robert Werner Oct 16 '19 at 16:45
2

You didn't post a complete mwe, so I've constructed one:

\documentclass{article}
\usepackage{array,longtable}
\begin{document}
some text before 
\begin{longtable}{ | p{2.0cm} | *{4}{>{\raggedright\arraybackslash}p{3.5cm} |} } 
\hline
\b{Columnhead} & \b{Columnhead} & \b{Columnhead} & \b{Columnhead} &
\b{Columnhead}  \\ 
\hline \endhead

Cell &

Cell \newline
Line \newline
Line \newline
Line - line n &

Cell &

Cell &

Cell \\

\hline

Cell &

Cell \newline
Line \newline
Line \newline
Line - line n &

Cell &

Cell &

Cell \\

\hline
\end{longtable}

misplaced cells
\end{document}

I would say that the extra column is caused by the fact that you define 6 columns in your sample, but then use only 5 columns in each row. As tex4ht uses CSS for drawing of the borders, it draws border also around this empty column.

To remove the empty rows, you can use a make4ht build file. Save the following code as build.lua:

local domfilter = require "make4ht-domfilter"

local process = domfilter {function(dom)
  local is_empty_row = function(row)
    local not_empty = false
    local element_count = 0
    -- ignore hline rows
    local row_class = row:get_attribute("class") 
    if row_class == "hline" or row_class == "cline" then return false end
    -- detect if the row contain only one empty child
    for _,child in ipairs(row:get_children() or {}) do
      if child:is_element() then 
        element_count = element_count + 1
        -- empty rows contain only one element, it is not empty otherwise
        if element_count > 1 then return false end
        -- detect if it contains only whitespace
        not_empty = child:get_text():gsub("%s","") ~= "" or not_empty
      end
    end
    -- print("element count", element_count, not_empty)
    return element_count == 1 and not_empty == false
  end
  for _, tbl in ipairs(dom:query_selector("table")) do
    -- find the empty rows
    for _, row in ipairs(tbl:query_selector("tr")) do
      if is_empty_row(row) then row:remove_node() end
    end

  end
  return dom
end}

Make:match("html$", process)

It uses LuaXML library to process all <table> elements and to remove all empty rows from tables.

One additional fix I would use is to correct rendering of the horizontal lines. Try this mycfg.cfg file:

\Preamble{xhtml}
\Css{tr.hline, tr.hline td, tr.cline, tr.cline td{padding:0;}}
\Css{tr.hline hr, tr.cline hr{border:none;border-top:1px solid black;}}
\begin{document}
\EndPreamble

Compile with

make4ht -c mycfg.cfg -e build.lua filename.tex

This is the result:

enter image description here

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.