diff --git a/column-div/Makefile b/column-div/Makefile new file mode 100644 index 00000000..da6ff833 --- /dev/null +++ b/column-div/Makefile @@ -0,0 +1,19 @@ +DIFF ?= diff --strip-trailing-cr -u + +.PHONY: test + +test: test_latex test_html + +test_html: sample.md expected.html column-div.lua + @pandoc -s --lua-filter column-div.lua --to=html $< \ + | $(DIFF) expected.html - + +test_latex: sample.md expected.tex column-div.lua + @pandoc --lua-filter column-div.lua --to=latex $< \ + | $(DIFF) expected.tex - + +expected.html: sample.md column-div.lua + pandoc -s --lua-filter column-div.lua --output $@ $< + +expected.tex: sample.md column-div.lua + pandoc --lua-filter column-div.lua --output $@ $< diff --git a/column-div/README.md b/column-div/README.md new file mode 100644 index 00000000..c16f7673 --- /dev/null +++ b/column-div/README.md @@ -0,0 +1,149 @@ +--- +title: "Column Div - leverage Pandoc native divs to make columns + and other things" +author: "Christophe Agathon" +--- + +Column Div +======= + +Columns and other things with Pandoc's markdown + +This Lua filter for Pandoc improves Pandoc's Div usage.Especially +fenced divs witten in Pandocs markdown. + +v1.0. Copyright: © 2021 Christophe Agathon + +License: MIT - see LICENSE file for details. + +Introduction +------------ +Pandoc fenced divs can be very powerful allowing providing, in +theory many document formating possibilities. Unfortunately, plain +Panfoc processing doesn't make full adventage of it and discards +some formating in HTML outputs and most of it in Latex outputs. + +Multiple columns in document are only partialy accessible in +Beamer (not plain Latex) and HTML outputs. + +As a result, it's not possible to render fancy multi columns +PDF document from markdown sources. + +The main purpose of this filter is to make it possible and give +similar formating features for both Latex/PDF and HTML outputs. + +My guidelines are : + +1) Use Pandoc divs like many already have proposed for uneven and even columns +2) Same functionalities and rendering in HTML and Latex+PDF +3) Mess the least possible with plain Pandoc processing which is quite OK already for HTML (miss only column-count for even columning). +4) Allow users to use unknown Latex environments from exotic packages if they wish, provided they include them in the preamble. + + +Usage +----- + +### Basic usage + +Copy `column-div.lua` in your document folder or in your pandoc +data directory (details in +[Pandoc's manual](https://pandoc.org/MANUAL.html#option--lua-filter)). +Run it on your document with a `--luafilter` option: + +```bash +pandoc --luafilter column-div.lua SOURCE.md -o OUTPUT.pdf + +``` + +or specify it in a defaults file (details in +[Pandoc's manual](https://pandoc.org/MANUAL.html#option--defaults)). + +This will generate consistent HTML, Latex and PDF outputs from +Pandoc markdown files. + +### Formating the document + +Everything is done with Pandoc's fenced divs with class names and +attributes. The attributes are similar to those from HTML styling and/or +Latex. + +#### Multiple even columns +For Latex and PDF output, you will need to call the multicol +package. This can be done un the YAML header. + +**Example:** + +```markdown +--- +header-includes: + - | + ```{=latex} + \usepackage{multicol} + + ``` +--- + +Some regular text + +:::: {.multicols column-count="2"} +Some text formatted on 2 columns +:::: +``` + +* Latex output is done with `multicols` environment. +* HTML output uses `style="column-count: 2"` on a div block. + +#### Uneven columns + +No specific Latex package are needed. We use Nested Pandoc divs in +the same way that columns and column environments are used in +Beamer/Latex. + +**Example:** + +```markdown + +:::::::: {.columns} +:::: {.column width="20%" valign="c"} +Some text or image using 20% of the page width. +:::: +:::: {.column width="80%" valign="c"} +Some text or image using 80% of the page with. +:::: +:::::::: +``` + +* Beamer/Latex output is based on columns and column environments +* Plain Latex (and PDF) rendering use minipage environments +* HTML rendering is not affected by this filter since Pandoc do it +well already (based on divs with `width` attributes). + +#### Other usages + +For HTML outputs, you already can create divs with whatever class names you +like and style them with `style=" … "` attributes. This is +processed by Pandoc and has nothing to do with this filter. + +This filter allows to do the same in Latex (and PDF). +You can create whatever environment you need. The environment name is the +class name given to the fenced div. In case of multiple class names, the +first one is used. Other are ignored but allowed to help you to maintain +a single markdown source for PDF and HTML outputs. +The `data-latex=" … "` attribute allows you to pass options and +parameters to the `\begin` environment instruction. + +To Do +----- + +Others multi column features could be implemented as column +spacing, rules, etc. + +Since Pandoc does a very good job with the `width` styling +attribute to implement variable column width, it could easily +support HTML even column via the `column-count` attribute. + +Contributing +------------ + +PRs welcome. + diff --git a/column-div/column-div.lua b/column-div/column-div.lua new file mode 100644 index 00000000..942d8052 --- /dev/null +++ b/column-div/column-div.lua @@ -0,0 +1,155 @@ +--[[ +column-div - leverage Pandoc native divs to make balanced and unbalanced column + and other things based on class name and attributes. + +Copyright: © 2021 Christophe Agathon +License: MIT – see LICENSE file for details + +Credits: Romain Lesur and Yihui Xie for the original column filter + implementation (output in beamer format). + +Output: latex, pdf, html + +Usage: classname attributes + balanced columns .columns column-count + columns(container) .columns + column(each column) .column width(percent) valign(t|c|b) + other divs . data-latex + + See README.md for details + +Note: You need to include multicol latex package to get balanced columns + in latex or pdf + I tried to use well known html or latex parameter. + Even if lua doen't like hyphens like in column-count. + +Bugs: * html rendering throws a warning [WARNING] Ignoring duplicate attribute style="width:60%;". + when width AND color are set and totally ignore the width + attribute. Don't know if this bug is mine +--]] +local List = require 'pandoc.List' + +function Div(div) + local options = '' + local env = '' + local returned_list + local begin_env + local end_env + local opt + + -- if the div has no class, the object is left unchanged + -- if the div has no class but an id, div.classes ~= nil + -- TODO: use a div with no class to build a 'scope' in Latex + -- usefull for user who would throw inline Latex code and limit it's + -- effect. + if not div.classes or #div.classes == 0 then return nil end + + -- if the format is latex then do minipage and others (like multicol) + if FORMAT:match 'latex' then + -- build the returned list of blocks + if div.classes:includes('column') then + env = 'column' + opt = div.attributes.width + if opt then + local width=tonumber(string.match(opt,'(%f[%d]%d[,.%d]*%f[%D])%%'))/100 + options = '{' .. tostring(width) + if div.attributes['background-color'] then + -- fix the width for the \colorbox + options = '{\\dimexpr' .. tostring(width) + .. '\\columnwidth-4\\fboxsep\\relax}' + else + options = '{' .. tostring(width) .. '\\columnwidth}' + end + end + + opt = div.attributes.valign + if opt then options = '[' .. opt .. ']' .. options end + + begin_env = List:new{pandoc.RawBlock('tex', + '\\begin{minipage}' .. options)} + end_env = List:new{pandoc.RawBlock('tex', '\\end{minipage}')} + + -- add support for color + opt = div.attributes.color + if opt then + begin_env = begin_env .. List:new{pandoc.RawBlock('tex', + '\\color{' .. opt .. '}')} + div.attributes.color = nil -- consume attribute + end + + opt = div.attributes['background-color'] + if opt then + begin_env = List:new{pandoc.RawBlock('tex', + '\\colorbox{' .. opt .. '}{')} + .. begin_env + end_env = end_env .. List:new{pandoc.RawBlock('tex', '}')} + div.attributes['background-color'] = nil -- consume attribute + end + + returned_list = begin_env .. div.content .. end_env + + elseif div.classes:includes('columns') then + -- it turns-out that asimple Tex \mbox do the job + begin_env = List:new{pandoc.RawBlock('tex', '\\mbox{')} + end_env = List:new{pandoc.RawBlock('tex', '}')} + returned_list = begin_env .. div.content .. end_env + + else + -- other environments ex: multicols + if div.classes:includes('multicols') then + env = 'multicols' + -- process supported options + opt = div.attributes['column-count'] + if opt then options = '{' .. opt .. '}' end + --[[ This functionality will be moved in another filter since it can't be consistent with the positionless classname requirement + else + -- Latex skilled users can use arbitrary environments passed as + -- the first (and only signifiant) class name. + env = div.classes[1] + -- default if no known options + if options == '' and div.attributes['data-latex'] then + options = div.attributes['data-latex'] + end + --]] + end + + begin_env = List:new{pandoc.RawBlock('tex', + '\\begin{' .. env .. '}' .. options)} + end_env = List:new{pandoc.RawBlock('tex', '\\end{' .. env .. '}')} + returned_list = begin_env .. div.content .. end_env + end + + -- if the format is html add what is not already done by plain pandoc + elseif FORMAT:match 'html' then + local style + -- add support for multi columns + opt = div.attributes['column-count'] + if opt then + -- add column-count to style + style = 'column-count: ' .. opt .. ';' .. (style or '') + div.attributes['column-count'] = nil + -- column-count is "consumed" by the filter otherwise it would appear as + -- data-column-count="…" in the resulting document + end + -- add support for color + opt = div.attributes.color + if opt then + -- add color to style + style = 'color: ' .. opt .. ';' .. (style or '') + div.attributes.color = nil -- consume attribute + end + opt = div.attributes['background-color'] + if opt then + -- add color to style + style = 'background-color: ' .. opt .. ';' .. (style or '') + div.attributes['background-color'] = nil -- consume attribute + end + -- if we have style then build returned list + if style then + div.attributes.style = style .. (div.attributes.style or '') + returned_list = List:new{pandoc.Div(div.content, div.attr)} + --returned_list = List:new{pandoc.Div(div.content)} + end + end + return returned_list +end diff --git a/column-div/expected.html b/column-div/expected.html new file mode 100644 index 00000000..3f7820d8 --- /dev/null +++ b/column-div/expected.html @@ -0,0 +1,216 @@ + + + + + + + Test + + + + + +
+

Test

+
+

column-div test

+

content…

+
+

content…

+
+

Three columns

+
+

content…

+

content…

+

content…

+
+

Two uneven columns

+
+
+

contents…

+
+

contents…

+
+
+

Columns in columns

+
+
+
+

contents…

+
+

contents…

+
+
+
+
+

contents…

+
+

contents…

+
+
+
+
+

contents…

+
+

contents…

+
+
+
+

Columns and Colors

+
+
+

blue content…

+
+

content on red background…

+
+
+
+
+

blue content on red background…

+
+

contents…

+
+
+ + diff --git a/column-div/expected.tex b/column-div/expected.tex new file mode 100644 index 00000000..a40af16b --- /dev/null +++ b/column-div/expected.tex @@ -0,0 +1,141 @@ +\hypertarget{column-div-test}{% +\section{column-div test}\label{column-div-test}} + +content\ldots{} + +\leavevmode\vadjust pre{\hypertarget{thisdivdoesnothing}{}}% +content\ldots{} + +\hypertarget{three-columns}{% +\subsection{Three columns}\label{three-columns}} + +\begin{multicols}{3} + +content\ldots{} + +content\ldots{} + +content\ldots{} + +\end{multicols} + +\hypertarget{two-uneven-columns}{% +\subsection{Two uneven columns}\label{two-uneven-columns}} + +\mbox{ + +\begin{minipage}[b]{0.4\columnwidth} + +contents\ldots{} + +\end{minipage} + +\begin{minipage}[b]{0.6\columnwidth} + +contents\ldots{} + +\end{minipage} + +} + +\hypertarget{columns-in-columns}{% +\subsection{Columns in columns}\label{columns-in-columns}} + +\begin{multicols}{3} + +\mbox{ + +\begin{minipage}[b]{0.2\columnwidth} + +contents\ldots{} + +\end{minipage} + +\begin{minipage}[b]{0.8\columnwidth} + +contents\ldots{} + +\end{minipage} + +} + +\mbox{ + +\begin{minipage}[b]{0.2\columnwidth} + +contents\ldots{} + +\end{minipage} + +\begin{minipage}[b]{0.8\columnwidth} + +contents\ldots{} + +\end{minipage} + +} + +\mbox{ + +\begin{minipage}[b]{0.2\columnwidth} + +contents\ldots{} + +\end{minipage} + +\begin{minipage}[b]{0.8\columnwidth} + +contents\ldots{} + +\end{minipage} + +} + +\end{multicols} + +\hypertarget{columns-and-colors}{% +\subsection{Columns and Colors}\label{columns-and-colors}} + +\mbox{ + +\begin{minipage}{0.4\columnwidth} + +\color{blue} + +blue content\ldots{} + +\end{minipage} + +\colorbox{red}{ + +\begin{minipage}{\dimexpr0.6\columnwidth-4\fboxsep\relax} + +content on red background\ldots{} + +\end{minipage} + +} + +} + +\mbox{ + +\colorbox{red}{ + +\begin{minipage}{\dimexpr0.6\columnwidth-4\fboxsep\relax} + +\color{blue} + +blue content on red background\ldots{} + +\end{minipage} + +} + +\begin{minipage}{0.4\columnwidth} + +contents\ldots{} + +\end{minipage} + +} diff --git a/column-div/sample.md b/column-div/sample.md new file mode 100644 index 00000000..a6a8d412 --- /dev/null +++ b/column-div/sample.md @@ -0,0 +1,85 @@ +--- +title : Test +header-includes: + - | + ```{=latex} + \usepackage{multicol} + + ``` +--- + +# column-div test +content... + +::: {#thisdivdoesnothing} +content... +::: + +## Three columns +::: {.anotherclassname .multicols column-count="3"} +content... + +content... + +content... +::: + +## Two uneven columns +:::::: {.columns} +::: {.column width="40%" valign="b"} +contents... +::: +::: {.column width="60%" valign="b"} +contents... +::: +:::::: + +## Columns in columns + +::::::::: {.multicols column-count="3"} +:::::: {.columns} +::: {.column width="20%" valign="b"} +contents... +::: +::: {.column width="80%" valign="b"} +contents... +::: +:::::: +:::::: {.columns} +::: {.column width="20%" valign="b"} +contents... +::: +::: {.column width="80%" valign="b"} +contents... +::: +:::::: +:::::: {.columns} +::: {.column width="20%" valign="b"} +contents... +::: +::: {.column width="80%" valign="b"} +contents... +::: +:::::: +::::::::: + + +## Columns and Colors + +:::::: {.columns} +::: {.column width="40%" color="blue"} +blue content... +::: +::: {.column width="60%" background-color="red"} +content on red background... +::: +:::::: + +:::::: {.columns} +::: {.column width="60%" color="blue" background-color="red"} +blue content on red background... +::: +::: {.column width="40%" } +contents... +::: +:::::: \ No newline at end of file diff --git a/tables-vrules/Makefile b/tables-vrules/Makefile new file mode 100644 index 00000000..ab1ab717 --- /dev/null +++ b/tables-vrules/Makefile @@ -0,0 +1,12 @@ +DIFF ?= diff --strip-trailing-cr -u + +.PHONY: test + +test: test_latex + +test_latex: sample.md expected.tex tables-rules.lua + @pandoc -s --lua-filter tables-rules.lua --to=latex $< \ + | $(DIFF) expected.tex - + +expected.tex: sample.md tables-rules.lua + pandoc -s --lua-filter tables-rules.lua --output $@ $< diff --git a/tables-vrules/README.md b/tables-vrules/README.md new file mode 100644 index 00000000..a62e468e --- /dev/null +++ b/tables-vrules/README.md @@ -0,0 +1,94 @@ +--- +title: "tables-rules - Pandoc filter to add rules to tables in LaTeX" +author: "Christophe Agathon" +--- + +Tables Rules +======= + +Add vertical and horizontal rules to tables. + +v1.0. Copyright: © 2021 Christophe Agathon + +License: MIT - see LICENSE file for details. + +Introduction +------------ + +This filter manages vertical and horizontal rules in latex generated by Pandoc. + +Two boolean metadata `tables-vrules` and `tables-hrules` control if tables will have vertical or horizontal rules, or both. + +History +------- + +Since pandoc has a strong policy against vertical rules in tables, people have been looking for solutions to get those, especially when rendering PDF files via Latex. + +For more information you can refer to : + +* This Pandoc issue [https://github.com/jgm/pandoc/issues/922](https://github.com/jgm/pandoc/issues/922) +* This discussion on StackExchange [https://tex.stackexchange.com/questions/595615/how-can-i-reformat-a-table-using-markdown-pandoc-pdf/596005](https://tex.stackexchange.com/questions/595615/how-can-i-reformat-a-table-using-markdown-pandoc-pdf/596005) + +marjinshraagen proposed a solution based on a patch of `\LT@array` in Latex. It used to work pretty well. It doesn't anymore for Multiline Tables and Pipes Tables since Pandoc changed the Latex code it generates for those kind of tables. Don't know exactly when it changed but sometime between Pandoc version 2.9.2.1 and version 2.16. + +Since patching in Latex is tricky and I am not a Latex guru, I didn't manage to make it work again, so I made this filter which change the call to `longtable` to add vertical rules in a more "natural" way. + +Note +---- + +### Préamble + +The filter adds some code in the Latex preamble because Pandoc adds it only if there are tables in the document (which is OK) and only if the Tables blocks are not processed by a filter (which might be considered as bug). + +### Filter order + +You would probably call this filter after your other filters since it generates raw LaTeX code for every table in the document. Pandoc blocs and inlines are no longer available for subsequent filters. + +This might be better to do this in a writer than in a filter unfortunately writers are not modular like filters and one would have to implement the whole LaTeX writer to do this. + +### Compatibility + +Since the processing is not based on a LaTeX \patchcmd, compatibility is high. +Its compatible with `colortbl` and `array` packages. + +Rules are not doubled below the table first row (header) and last row as it happens with others hacks. + +Usage +----- + +### Formating the document + +Simply use one of the table synthax allowed by Pandoc (details in +[Pandoc's manual](https://pandoc.org/MANUAL.html#tables). + +Set `tables-vrules: true` and/or `tables-hrules: true` in the YAML preamble to get rules on all the tables of the document. + + +### Rendering the document + +Copy `tables-rules.lua` in your document folder or in your pandoc +data directory (details in +[Pandoc's manual](https://pandoc.org/MANUAL.html#option--lua-filter)). +Run it on your document with a `--luafilter` option: + +```bash +pandoc --luafilter tables-rules.lua SOURCE.md -o OUTPUT.pdf + +``` + +or specify it in a defaults file (details in +[Pandoc's manual](https://pandoc.org/MANUAL.html#option--defaults)). + +This will generate Tables with vertical rules in Latex and PDF documents from Pandoc markdown source files. + +### Limitations + +This filter is active only for Latex and PDF output. + +Rules in HTML documents should be handled via css styling. + +Contributing +------------ + +PRs welcome. + diff --git a/tables-vrules/expected.tex b/tables-vrules/expected.tex new file mode 100644 index 00000000..1c4f180b --- /dev/null +++ b/tables-vrules/expected.tex @@ -0,0 +1,213 @@ +% Options for packages loaded elsewhere +\PassOptionsToPackage{unicode}{hyperref} +\PassOptionsToPackage{hyphens}{url} +% +\documentclass[ +]{article} +\usepackage{amsmath,amssymb} +\usepackage{lmodern} +\usepackage{iftex} +\ifPDFTeX + \usepackage[T1]{fontenc} + \usepackage[utf8]{inputenc} + \usepackage{textcomp} % provide euro and other symbols +\else % if luatex or xetex + \usepackage{unicode-math} + \defaultfontfeatures{Scale=MatchLowercase} + \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1} +\fi +% Use upquote if available, for straight quotes in verbatim environments +\IfFileExists{upquote.sty}{\usepackage{upquote}}{} +\IfFileExists{microtype.sty}{% use microtype if available + \usepackage[]{microtype} + \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts +}{} +\makeatletter +\@ifundefined{KOMAClassName}{% if non-KOMA class + \IfFileExists{parskip.sty}{% + \usepackage{parskip} + }{% else + \setlength{\parindent}{0pt} + \setlength{\parskip}{6pt plus 2pt minus 1pt}} +}{% if KOMA class + \KOMAoptions{parskip=half}} +\makeatother +\usepackage{xcolor} +\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available +\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}} +\hypersetup{ + pdftitle={table-vrules lua filter test file}, + pdfauthor={Christophe Agathon}, + hidelinks, + pdfcreator={LaTeX via pandoc}} +\urlstyle{same} % disable monospaced font for URLs +\setlength{\emergencystretch}{3em} % prevent overfull lines +\providecommand{\tightlist}{% + \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}} +\setcounter{secnumdepth}{-\maxdimen} % remove section numbering +%begin tables-vrules.lua +\usepackage{longtable,booktabs,array} +\usepackage{calc} % for calculating minipage widths +% Correct order of tables after \paragraph or \subparagraph +\usepackage{etoolbox} +\makeatletter +\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{} +\makeatother +% Allow footnotes in longtable head/foot +\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}} +\makesavenoteenv{longtable} +\setlength{\aboverulesep}{0pt} +\setlength{\belowrulesep}{0pt} +\renewcommand{\arraystretch}{1.3} +%end tables-vrules.lua +\ifLuaTeX + \usepackage{selnolig} % disable illegal ligatures +\fi + +\title{table-vrules lua filter test file} +\author{Christophe Agathon} +\date{} + +\begin{document} +\maketitle + +\hypertarget{simple-table}{% +\section{Simple Table}\label{simple-table}} + +\begin{longtable}[]{@{}|r|l|c|l|@{}} +\caption{Demonstration of simple table syntax.}\tabularnewline +\toprule +Right & Left & Center & Default \\ +\midrule +\endfirsthead +\toprule +Right & Left & Center & Default \\ +\midrule +\endhead +12 & 12 & 12 & 12 \\ + +\midrule +123 & 123 & 123 & 123 \\ + +\midrule +1 & 1 & 1 & 1 \\ +\bottomrule +\end{longtable} + +\hypertarget{multiline-tables}{% +\section{Multiline Tables}\label{multiline-tables}} + +\begin{longtable}[]{@{} + |>{\centering\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1667}} + |>{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.1111}} + |>{\raggedleft\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.2222}} + |>{\raggedright\arraybackslash}p{(\columnwidth - 6\tabcolsep) * \real{0.3611}}|@{}} +\caption{Here's the caption. It, too, may span multiple +lines.}\tabularnewline +\toprule +\begin{minipage}[b]{\linewidth}\centering +Centered Header +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Default Aligned +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedleft +Right Aligned +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Left Aligned +\end{minipage} \\ +\midrule +\endfirsthead +\toprule +\begin{minipage}[b]{\linewidth}\centering +Centered Header +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Default Aligned +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedleft +Right Aligned +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Left Aligned +\end{minipage} \\ +\midrule +\endhead +First & row & 12.0 & Example of a row that spans multiple lines. \\ + +\midrule +Second & row & 5.0 & Here's another one. Note the blank line between +rows. \\ +\bottomrule +\end{longtable} + +\hypertarget{grid-tables}{% +\section{Grid Tables}\label{grid-tables}} + +\begin{longtable}[]{@{} + |>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.2222}} + |>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.2222}} + |>{\raggedright\arraybackslash}p{(\columnwidth - 4\tabcolsep) * \real{0.2917}}|@{}} +\caption{Sample grid table.}\tabularnewline +\toprule +\begin{minipage}[b]{\linewidth}\raggedright +Fruit +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Price +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Advantages +\end{minipage} \\ +\midrule +\endfirsthead +\toprule +\begin{minipage}[b]{\linewidth}\raggedright +Fruit +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Price +\end{minipage} & \begin{minipage}[b]{\linewidth}\raggedright +Advantages +\end{minipage} \\ +\midrule +\endhead +Bananas & \$1.34 & \begin{minipage}[t]{\linewidth}\raggedright +\begin{itemize} +\tightlist +\item + built-in wrapper +\item + bright color +\end{itemize} +\end{minipage} \\ + +\midrule +Oranges & \$2.10 & \begin{minipage}[t]{\linewidth}\raggedright +\begin{itemize} +\tightlist +\item + cures scurvy +\item + tasty +\end{itemize} +\end{minipage} \\ +\bottomrule +\end{longtable} + +\hypertarget{pipes-tables}{% +\section{Pipes Tables}\label{pipes-tables}} + +\begin{longtable}[]{@{}|r|l|l|c|@{}} +\caption{Demonstration of pipe table syntax.}\tabularnewline +\toprule +Right & Left & Default & Center \\ +\midrule +\endfirsthead +\toprule +Right & Left & Default & Center \\ +\midrule +\endhead +12 & 12 & 12 & 12 \\ + +\midrule +123 & 123 & 123 & 123 \\ + +\midrule +1 & 1 & 1 & 1 \\ +\bottomrule +\end{longtable} + +\end{document} diff --git a/tables-vrules/sample.md b/tables-vrules/sample.md new file mode 100644 index 00000000..71ecb6e6 --- /dev/null +++ b/tables-vrules/sample.md @@ -0,0 +1,58 @@ +--- +title: table-vrules lua filter test file +author: Christophe Agathon +tables-vrules: true +tables-hrules: true + +--- + +# Simple Table + + Right Left Center Default +------- ------ ---------- ------- + 12 12 12 12 + 123 123 123 123 + 1 1 1 1 + +Table: Demonstration of simple table syntax. + +# Multiline Tables + +------------------------------------------------------------- + Centered Default Right Left + Header Aligned Aligned Aligned +----------- ------- --------------- ------------------------- + First row 12.0 Example of a row that + spans multiple lines. + + Second row 5.0 Here's another one. Note + the blank line between + rows. +------------------------------------------------------------- + +Table: Here's the caption. It, too, may span +multiple lines. + +# Grid Tables + +: Sample grid table. + ++---------------+---------------+--------------------+ +| Fruit | Price | Advantages | ++===============+===============+====================+ +| Bananas | $1.34 | - built-in wrapper | +| | | - bright color | ++---------------+---------------+--------------------+ +| Oranges | $2.10 | - cures scurvy | +| | | - tasty | ++---------------+---------------+--------------------+ + +# Pipes Tables + +| Right | Left | Default | Center | +|------:|:-----|---------|:------:| +| 12 | 12 | 12 | 12 | +| 123 | 123 | 123 | 123 | +| 1 | 1 | 1 | 1 | + + : Demonstration of pipe table syntax. \ No newline at end of file diff --git a/tables-vrules/tables-rules.lua b/tables-vrules/tables-rules.lua new file mode 100644 index 00000000..627c17ce --- /dev/null +++ b/tables-vrules/tables-rules.lua @@ -0,0 +1,114 @@ + --[[ +tables-vrules - adds vertical rules to tables for latex output + +Copyright: © 2021 Christophe Agathon + +License: MIT – see LICENSE file for details + +Credits: marijnschraagen for the original Latex hack + +Output: latex, pdf. + +Usage: See README.md for details + +--]] +local List = require 'pandoc.List' + +local vars = {} + +function get_vars (meta) + vars.vrules = meta['tables-vrules'] + vars.hrules = meta['tables-hrules'] +end + +function repl_midrules(m1, m2) + if m2:match('^\\[%w]+rule') then + -- don't double the rule + return m1 .. m2 + else + return m1 .. '\n\\midrule\n' .. m2 + end +end + +function Table(table) + local returned_list + local latex_code = '' + local coldef ='' + local envdef ='' + local new_coldef ='' + local end_line = '' + + if not vars.vrules and not vars.hrules then return nil end + + if FORMAT:match 'latex' then + + -- Get latex code for the whole table + latex_code = pandoc.write ( pandoc.Pandoc({table}),'latex' ) + + -- Rewrite column definition to add vertical rules if needed + if vars.vrules then + envdef, begdef, coldef, enddef = + latex_code:match("((\\begin{longtable}%[[^%]]*%]{@{})(.*)(@{}}))") + + if coldef then + if coldef:match('^[lrc]+$') then + -- old style + new_coldef = coldef:gsub('(.)', '|%1') .. '|' + else + -- asuming new style + new_coldef = coldef:gsub('(>)', '|%1') .. '|' + end + latex_code = latex_code:sub(envdef:len() + 1) + end + end + + -- Add \midrules after each row if needed + if vars.hrules then + latex_code = latex_code:gsub('(\\\\\n)([\\%w]+)', repl_midrules) + end + + -- Return modified latex code as a raw block + if vars.vrules then + returned_list = List:new{pandoc.RawBlock('tex', + begdef .. new_coldef .. enddef .. + latex_code)} + else + returned_list = List:new{pandoc.RawBlock('tex', latex_code)} + end + end + return returned_list +end + +function Meta(meta) + -- We have to add this since Pandoc doesn't because there are no + -- table anymore in the AST. We converted them in RawBlocks + + if not vars.vrules and not vars.hrules then return nil end + includes = [[ +%begin tables-vrules.lua +\usepackage{longtable,booktabs,array} +\usepackage{calc} % for calculating minipage widths +% Correct order of tables after \paragraph or \subparagraph +\usepackage{etoolbox} +\makeatletter +\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{} +\makeatother +% Allow footnotes in longtable head/foot +\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}} +\makesavenoteenv{longtable} +\setlength{\aboverulesep}{0pt} +\setlength{\belowrulesep}{0pt} +\renewcommand{\arraystretch}{1.3} +%end tables-vrules.lua +]] + + if meta['header-includes'] then + table.insert(meta['header-includes'], pandoc.RawBlock('tex', includes)) + else + meta['header-includes'] = List:new{pandoc.RawBlock('tex', includes)} + end + + return meta +end + +return {{Meta = get_vars}, {Table = Table}, {Meta = Meta}}