Skip to content

Commit

Permalink
Improve LaTeX output for tables. [#96 state:resolved]
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarber committed Jan 29, 2009
1 parent 974d39b commit 9761708
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
=== Edge

* Improve LaTeX output for tables. #96

* Fix bad parsing of bracketed image links (which would hang the interpreter in some cases). #97

* Handle links containing parentheses. Brackets are no longer required. #82 [Ryan Alyea]
Expand Down
38 changes: 32 additions & 6 deletions lib/redcloth/formatters/latex.rb
Expand Up @@ -102,24 +102,50 @@ def p(opts)
end

def td(opts)
"\t\t\t#{opts[:text]} &\n"
column = @table_row.size
if opts[:colspan]
opts[:text] = "\\multicolumn{#{opts[:colspan]}}{ #{"l " * opts[:colspan].to_i}}{#{opts[:text]}}"
end
if opts[:rowspan]
@table_multirow_next[column] = opts[:rowspan].to_i - 1
opts[:text] = "\\multirow{#{opts[:rowspan]}}{*}{#{opts[:text]}}"
end
@table_row.push(opts[:text])
return ""
end

def tr_open(opts)
"\t\t"
@table_row = []
return ""
end

def tr_close(opts)
"\t\t\\\\\n"
multirow_columns = @table_multirow.find_all {|c,n| n > 0}
multirow_columns.each do |c,n|
@table_row.insert(c,"")
@table_multirow[c] -= 1
end
@table_multirow.merge!(@table_multirow_next)
@table_multirow_next = {}
@table.push(@table_row)
return ""
end

# FIXME: we need to know the column count before opening tabular context.
# We need to know the column count before opening tabular context.
def table_open(opts)
"\\begin{align*}\n"
@table = []
@table_multirow = {}
@table_multirow_next = {}
return ""
end

def table_close(opts)
"\t\\end{align*}\n"
output = "\\begin{tabular}{ #{"l " * @table[0].size }}\n"
@table.each do |row|
output << " #{row.join(" & ")} \\\\\n"
end
output << "\\end{tabular}\n"
output
end

def bc_open(opts)
Expand Down
77 changes: 73 additions & 4 deletions test/table.yml
Expand Up @@ -18,6 +18,13 @@ html: |-
</tr>
</table>
<h3>A header after the table</h3>
latex: |+
\begin{tabular}{ l l l }
a & b & c \\
1 & 2 & 3 \\
\end{tabular}
\subsubsection*{A header after the table}
---
in: |
|_. a|_. b|_. c|
Expand Down Expand Up @@ -56,6 +63,11 @@ html: |-
<td>row</td>
</tr>
</table>
latex: |+
\begin{tabular}{ l l l l l }
This & is & a & simple & table \\
This & is & a & simple & row \\
\end{tabular}
---
in: |-
table{border:1px solid black}.
Expand Down Expand Up @@ -144,25 +156,82 @@ html: |-
</table>
---
in: |-
|{background:#ddd}. Cell with gray background|
|{background:#ddd}. Cell with gray background|Normal cell|
|\2. Cell spanning 2 columns|
|/3. Cell spanning 3 rows|
|>. Right-aligned cell|
|/2. Cell spanning 2 rows|one|
|two|
|>. Right-aligned cell|<. Left-aligned cell|
html: |-
<table>
<tr>
<td style="background:#ddd;">Cell with gray background</td>
<td>Normal cell</td>
</tr>
<tr>
<td colspan="2">Cell spanning 2 columns</td>
</tr>
<tr>
<td rowspan="3">Cell spanning 3 rows</td>
<td rowspan="2">Cell spanning 2 rows</td>
<td>one</td>
</tr>
<tr>
<td>two</td>
</tr>
<tr>
<td style="text-align:right;">Right-aligned cell</td>
<td style="text-align:left;">Left-aligned cell</td>
</tr>
</table>
latex: |+
\begin{tabular}{ l l }
Cell with gray background & Normal cell \\
\multicolumn{2}{ l l }{Cell spanning 2 columns} \\
\multirow{2}{*}{Cell spanning 2 rows} & one \\
& two \\
Right-aligned cell & Left-aligned cell \\
\end{tabular}
---
name: row spanning mid-row
in: |-
|1|2|3|
|1|/3. 2|3|
|1|3|
|1|3|
|1|2|3|
html: |-
<table>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td rowspan="3">2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</table>
latex: |+
\begin{tabular}{ l l l }
1 & 2 & 3 \\
1 & \multirow{3}{*}{2} & 3 \\
1 & & 3 \\
1 & & 3 \\
1 & 2 & 3 \\
\end{tabular}
---
in: |
{background:#ddd}. |S|Target|Complete|App|Milestone|
Expand Down

0 comments on commit 9761708

Please sign in to comment.