Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to prevent colgroup column width ratios being added to pipe tables #8139

Closed
daviewales opened this issue Jun 21, 2022 · 12 comments

Comments

@daviewales
Copy link

daviewales commented Jun 21, 2022

If you create a markdown table using the pipe tables extension, and any line of the markdown source is more than the column width, then the relative cell widths in the output are calculated based on the ratio of dashes under the headings in the markdown.

For HTML output, this results in the insertion of an HTML colgroup block which forces the proportions of the cells.

However, I'm using R Markdown to insert computed values into my table cells, which means that the cells could realisticly have any number of characters in them, and I'd prefer to rely on the default column widths that the tables would have if there were no colgroup block at all.

I understand that if I use any of the other table extensions I'll get this, but as I'm inserting computed column values it makes the other table exentions unviable, as they rely on getting the spacing lined up exactly right, which I can't do here.

The workaround I have found to prevent this is to use the pandoc argument --columns 1000 , but it would be nice to be able to explicitly disable this feature.

@tarleb
Copy link
Collaborator

tarleb commented Jun 21, 2022

Here's a Lua filter that does this:

function Table (tbl)
  tbl.colspecs = tbl.colspecs:map(function (colspec)
      local align = colspec[1]
      local width = nil  -- default width
      return {align, width}
  end)
  return tbl
end

See the Manipulate Markdown via Pandoc Lua filters in the R Markdown Cookbook for more info on this approach.

@jgm
Copy link
Owner

jgm commented Jun 21, 2022

and any line of the markdown source is less than the column width

I think you meant "more than the column width."

@jgm
Copy link
Owner

jgm commented Jun 21, 2022

I think the filter solution is probably adequate. It's more complicated if you need this to apply to some but not all tables, but perhaps then you could enclose the relevant tables in a Div?

@daviewales
Copy link
Author

Thanks @tarleb and @jgm, the filter does exactly what I want it to do.
Personally I think this would be more discoverable if it were a pandoc option (or if there were another type of pipe table where this is the default).

However, if this is not preferred, perhaps the docs could be updated to point to this filter solution to make it easier to find?

@daviewales
Copy link
Author

daviewales commented Jul 1, 2022

Also, I've just discovered that this filter doesn't work with older versions such as Pandoc 2.5 (pandoc-types 1.17.5.4) which is the default on Ubuntu 20.04. Is there a way to fix the filter to work on older versions?

I'm getting this error:

pandoc -s -o test.html test.md --lua-filter filter-html-auto-table-column-widths.lua
Error running filter filter-html-auto-table-column-widths.lua:
filter-html-auto-table-column-widths.lua:4: attempt to index a nil value (field 'colspecs')
stack traceback:
        filter-html-auto-table-column-widths.lua:4: in function 'Table'

I'm guessing that colspecs doesn't exist in older versions of pandoc?

@tarleb
Copy link
Collaborator

tarleb commented Jul 3, 2022

I believe that this version should work with both pandoc versions, but I didn't test it:

function Table (tbl)
  if PANDOC_VERSION >= '2.10' then
    tbl.colspecs = tbl.colspecs:map(function (colspec)
        local align = colspec[1]
        local width = nil  -- default width
        return {align, width}
    end)
  else
    for i, w in ipairs(tbl.widths) do
      tbl.widths[i] = 0
    end
  end
  return tbl
end

@tarleb
Copy link
Collaborator

tarleb commented Jul 3, 2022

Related issue: #2165

@daviewales
Copy link
Author

Thanks @tarleb. I had to tweak it slightly as I was getting the error attempt to compare string with table. The following works:

-- Unset the width attribute of HTML colspecs in tables
-- See https://github.com/jgm/pandoc/issues/8139
function Table (tbl)
  if PANDOC_VERSION[1] >= 2 and PANDOC_VERSION[2] >= 10 then
    tbl.colspecs = tbl.colspecs:map(function (colspec)
        local align = colspec[1]
        local width = nil  -- default width
        return {align, width}
    end)
  else
    for i, w in ipairs(tbl.widths) do
      tbl.widths[i] = 0
    end
  end
  return tbl
end

jgm added a commit that referenced this issue Jul 4, 2022
@jgm
Copy link
Owner

jgm commented Jul 4, 2022

I'm adding a FAQ entry which should make this more discoverable.

@jgm jgm closed this as completed Jul 4, 2022
@gregdan3
Copy link

Just ran into this myself.

The issue stood out badly because I'm using ligatures with wildly different sizes from the original text, but of course pandoc can't know I'm providing a font with ligatures when it gets to the CSS.

2022-09-25T19:13:16-05:00
This is Toki Pona. Third from the top is the joke word "kijetesantakalu", which demonstrates the issue nicely!

2022-09-25T19:15:06-05:00
For comparison, normal-appearing ligatures (still affected, but the underlying words are more consistent in length).

@binyomen
Copy link

binyomen commented Feb 8, 2023

The version check in the code above doesn't seem to work once you get to Pandoc version 3, since the minor version is no longer 10 or greater. I think what we want is:

-- Unset the width attribute of HTML colspecs in tables
-- See https://github.com/jgm/pandoc/issues/8139
function Table (tbl)
  if PANDOC_VERSION[1] > 2 or (PANDOC_VERSION[1] == 2 and PANDOC_VERSION[2] >= 10) then
    tbl.colspecs = tbl.colspecs:map(function (colspec)
        local align = colspec[1]
        local width = nil  -- default width
        return {align, width}
    end)
  else
    for i, w in ipairs(tbl.widths) do
      tbl.widths[i] = 0
    end
  end
  return tbl
end

This should work until a future breaking change alters how column specs are accessed.

@Julynx
Copy link

Julynx commented Apr 2, 2024

I was able to work around this by adding:

/* style.css */
col {
  width: auto !important;
}

To the CSS stylesheet, then passing --css style.css

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants