Wiki source code of Render Table Cells in Italic

Last modified by Vincent Massol on 2026/08/30 19:49

Hide last authors
Vincent Massol 2.1 1 Every body cell of every table is written by the ##TableCellBlock## template, so a ##latex/TableCellBlock## template of your own [[takes its place>>doc:documentation.extensions.dev.latex.template-mechanism.WebHome]] 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.
2
3 1. 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.(((
4 {{code language="velocity"}}
5 #set ($currentCellBlock = $latex.block)
6 #set ($class = $latex.block.getParameter('class'))
7 ## Support specifying some cell text to be displayed in a smaller font
8 ## Example: |(% class="small" %)cell|
9 #if ($class.equals('small'))
10 {\small ##
11 #end
12 ${latex.processor.process($latex.block.getChildren())}##
13 #if ($latex.tool.isTableCell($currentCellBlock.getNextSibling()))
14 ${SP}&${SP}##
15 #end
16 {{/code}}
17
18 Two 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()##>>doc:documentation.extensions.dev.latex.template-bindings.WebHome]], 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.
19 )))
20 1. 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>>doc:documentation.extensions.dev.latex.override-template.WebHome]]. 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.(((
21 {{code language="velocity"}}
22 ## Step 1: get the default template being overridden
23 #set ($template = $latex.processor.getTemplate('default/TableCellBlock'))
24 ## Step 2: compute the new content
25 #set ($original = '${latex.processor.process($latex.block.getChildren())}')
26 #set ($italic = '\textit{${latex.processor.process($latex.block.getChildren())}}')
27 #set ($newContent = $stringtool.replace($template.content.content, $original, $italic))
28 ## Step 3: set the new content
29 #set ($discard = $template.content.setContent($newContent))
30 ## Step 4: render the modified template
31 $latex.processor.render($template)##
32 {{/code}}
33 )))
34 1. Install it under the path ##latex/TableCellBlock##, [[the same way as any other override>>doc:documentation.extensions.dev.latex.override-template.WebHome]].
35 1. 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.(((
36 {{code language="none"}}
37 |=Name|=Role
38 |John|Author
39 |(% class="small" %)Ana|Reviewer
40 {{/code}}
41
42 Every 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.
43
44 {{code language="tex"}}
45 \begin{center}
46 \begin{tabular}{|l|l|}
47 \hline
48 \textbf{Name} & \textbf{Role}\\
49 \hline
50 \textit{John} & \textit{Author}\\
51 \hline
52 {\small \textit{Ana}} & \textit{Reviewer}\\
53 \hline
54 \end{tabular}
55 \end{center}
56 {{/code}}
57 )))

Get Connected