Render Table Cells in Italic
Tutorial
Every body cell of every table is written by the TableCellBlock template, so a latex/TableCellBlock template of your own takes its place and changes them all at once. That template does more than write the cell's text, though, and this tutorial keeps the rest of it: it loads the shipped one, wraps the single call that writes the content in \textit{}, and renders the result.
- Read the template you are about to override. It is templates/latex/default/TableCellBlock in the extension's jar, and it does three things — an optional smaller font, the cell's content, and the & that separates this cell from the next one.
#set ($currentCellBlock = $latex.block) #set ($class = $latex.block.getParameter('class')) ## Support specifying some cell text to be displayed in a smaller font ## Example: |(% class="small" %)cell| #if ($class.equals('small')) {\small ## #end ${latex.processor.process($latex.block.getChildren())}## #if ($latex.tool.isTableCell($currentCellBlock.getNextSibling())) ${SP}&${SP}## #endTwo habits in there are worth taking away. A row's cells are siblings, so "am I the last cell?" is asked by testing the next sibling with $latex.tool.isTableCell(), which answers for header cells too, and the & is written only when there is one. And every line whose output must not end in a newline closes with a Velocity line comment — the two hashes ending the content line are what keep the cell, its separator and the next cell on a single line.
- Wrap the content in \textit{}. Take the shipped template with getTemplate('default/TableCellBlock'), replace just the call that writes the content, and render what comes back — the four-step override strategy. Matching that one call rather than the whole line is what leaves the small-font branch, the separator and the line comment after it untouched.
## Step 1: get the default template being overridden #set ($template = $latex.processor.getTemplate('default/TableCellBlock')) ## Step 2: compute the new content #set ($original = '${latex.processor.process($latex.block.getChildren())}') #set ($italic = '\textit{${latex.processor.process($latex.block.getChildren())}}') #set ($newContent = $stringtool.replace($template.content.content, $original, $italic)) ## Step 3: set the new content #set ($discard = $template.content.setContent($newContent)) ## Step 4: render the modified template $latex.processor.render($template)## - Install it under the path latex/TableCellBlock, the same way as any other override.
- Export a page holding a table and read its .tex file. Take one with a class="small" cell in it, so that the two effects show together.
|=Name|=Role |John|Author |(% class="small" %)Ana|ReviewerEvery body cell is now italic, the separators sit where they were, and the small cell kept its smaller font on top of the italic — which is the payoff of having replaced one call instead of the whole template.
\begin{center} \begin{tabular}{|l|l|} \hline \textbf{Name} & \textbf{Role}\\ \hline \textit{John} & \textit{Author}\\ \hline {\small \textit{Ana}} & \textit{Reviewer}\\ \hline \end{tabular} \end{center}
FAQ
Why are the header cells still bold?
A header cell is a TableHeadCellBlock, a different Block with a template of its own, so latex/TableCellBlock never sees it. The shipped TableHeadCellBlock wraps its content in \textbf{}; running the same four steps against default/TableHeadCellBlock puts the headers in italic as well, or in both.
What happens if I drop the line comment after the content?
The cell's text then ends in a newline, and each & and each row-ending \\ lands on a line of its own. LaTeX ignores that whitespace, so the document still compiles and still looks right — but the generated .tex becomes much harder to read when an export needs debugging.