Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - LaTeX typesetting part 2 (tables)

#1
LaTeX typesetting part 2 (tables)

<div style="margin: 5px 5% 10px 5%;"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables.png" width="922" height="145" title="" alt="" /></div><div><p>LaTeX offers a number of tools to create and customise tables, in this series we will be using the tabular and tabularx environment to create and customise tables.</p>
<p> <span id="more-31250"></span> </p>
<h2>Basic table</h2>
<p>To create a table you simply specify the environment \begin{tabular}{columns}</p>
<pre class="wp-block-preformatted">\begin{tabular}{c|c} Release &amp;;\;Codename \\ \hline Fedora Core 1 &amp;;Yarrow \\ Fedora Core 2 &amp;;Tettnang \\ Fedora Core 3 &amp;;Heidelberg \\ Fedora Core 4 &amp;;Stentz \\ \end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables.png" alt="" /></figure>
<p>In the above example “{c|c}” in the curly bracket refers to the position of the text in the column. The below table summarises the positional argument together with the description.</p>
<div class="wp-block-jetpack-markdown">
<table>
<thead>
<tr>
<th style="text-align:center">Position</th>
<th>Argument</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">c</td>
<td>Position text in the centre</td>
</tr>
<tr>
<td style="text-align:center">l</td>
<td>Position text left-justified</td>
</tr>
<tr>
<td style="text-align:center">r</td>
<td>Position text right-justified</td>
</tr>
<tr>
<td style="text-align:center">p{width}</td>
<td>Align the text at the top of the cell</td>
</tr>
<tr>
<td style="text-align:center">m{width}</td>
<td>Align the text in the middle of the cell</td>
</tr>
<tr>
<td style="text-align:center">b{width}</td>
<td>Align the text at the bottom of the cell</td>
</tr>
</tbody>
</table>
<p>Both m{width} and b{width} requires the array package to be specified in the preamble.</p>
<p>Using the example above, let us breakdown the important points used and describe a few more options that you will see in this series</p>
<table>
<thead>
<tr>
<th style="text-align:center">Option</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">&amp;;</td>
<td>Defines each cell, the ampersand is only used from the second column</td>
</tr>
<tr>
<td style="text-align:center">\</td>
<td>This terminates the row and start a new row</td>
</tr>
<tr>
<td style="text-align:center">|</td>
<td>Specifies the vertical line in the table (optional)</td>
</tr>
<tr>
<td style="text-align:center">\hline</td>
<td>Specifies the horizontal line (optional)</td>
</tr>
<tr>
<td style="text-align:center">*{num}{form}</td>
<td>This is handy when you have many columns and is an efficient way of limiting the repetition</td>
</tr>
<tr>
<td style="text-align:center">||</td>
<td>Specifies the double vertical line</td>
</tr>
</tbody>
</table>
</div>
<div class="wp-block-group">
<div class="wp-block-group__inner-container">
<h2>Customizing a table</h2>
</div>
</div>
<p>Now that some of the options available are known, let us create a table using the options described in the previous section.</p>
<pre class="wp-block-preformatted">\begin{tabular}{*{3}{|l|}}
\hline \textbf{Version} &amp;;\textbf{Code name} &amp;;\textbf{Year released} \\
\hline Fedora 6 &amp;;Zod &amp;;2006 \\ \hline Fedora 7 &amp;;Moonshine &amp;;2007 \\ \hline Fedora 8 &amp;;Werewolf &amp;;2007 \\
\hline
\end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-1.png" alt="" /></figure>
<h2>Managing long text</h2>
<p>With LaTeX, if there are many texts in a column it will not be formatted well and does not look presentable.</p>
<p>The below example shows how long text is formatted, we will use “blindtext” in the preamble so that we can produce sample text.</p>
<pre class="wp-block-preformatted">\begin{tabular}{|l|l|}\hline Summary &amp;;Description \\ \hline Test &amp;;\blindtext \\
\end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-2.png" alt="" /></figure>
<p>As you can see the text exceeds the page width; however, there are a couple of options to overcome this challenge.</p>
<ul>
<li>Specify the column width, for example, m{5cm}</li>
<li>Utilise the tabularx environment, this requires tabularx package in the preamble.</li>
</ul>
<h3>Managing long text with column width</h3>
<p>By specifying the column width the text will be wrapped into the width as shown in the example below.</p>
<pre class="wp-block-preformatted">\begin{tabular}{|l|m{14cm}|} \hline Summary &amp;;Description \\ \hline Test &amp;;\blindtext \\ \hline
\end{tabular}\vspace{3mm}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-3.png" alt="" /></figure>
<h3>Managing long text with tabularx</h3>
<p>Before we can leverage tabularx we need to add it in the preamble. Tabularx takes the following example</p>
<pre class="wp-block-preformatted">**\begin{tabularx}{width}{columns}** \begin{tabularx}{\textwidth}{|l|X|} \hline
Summary &amp;; Tabularx Description\\ \hline
Text &amp;;\blindtext \\ \hline
\end{tabularx}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-4.png" alt="" /></figure>
<p>Notice that the column that we want the long text to be wrapped has a capital “X” specified.</p>
<h2>Multi-row and multi-column</h2>
<p>There are times when you will need to merge rows and/or column. This section describes how it is accomplished. To use multi-row and multi-column add multi-row to the preamble.</p>
<h3>Multirow</h3>
<p>Multirow takes the following argument <em>\multirow{number_of_rows}{width}{text}</em>, let us look at the below example.</p>
<pre class="wp-block-preformatted">\begin{tabular}{|l|l|}\hline Release &amp;;Codename \\ \hline Fedora Core 4 &amp;Stentz \\ \hline \multirow{2}{*}{MultiRow} &amp;;Fedora 8 \\ &amp;;Werewolf \\ \hline
\end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-5.png" alt="" /></figure>
<p>In the above example, two rows were specified, the ‘*’ tells LaTeX to automatically manage the size of the cell.</p>
<h3>Multicolumn</h3>
<p>Multicolumn argument is <em>\multicolumn{number_of_columns}{cell_position}{text}</em>, below example demonstrates multicolumn.</p>
<pre class="wp-block-preformatted">\begin{tabular}{|l|l|l|}\hline Release &amp;;Codename &amp;;Date \\ \hline Fedora Core 4 &amp;;Stentz &amp;;2005 \\ \hline \multicolumn{3}{|c|}{Mulit-Column} \\ \hline
\end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-6.png" alt="" /></figure>
<h2>Working with colours</h2>
<p>Colours can be assigned to the text, an individual cell or the entire row. Additionally, we can configure alternating colours for each row.</p>
<p>Before we can add colour to our tables we need to include <em>\usepackage[table]{xcolor}</em> into the preamble. We can also define colours using the following colour reference <a href="https://latexcolor.com">LaTeX Colour</a> or by adding an exclamation after the colour prefixed by the shade from 0 to 100. For example, <em>gray!30</em></p>
<pre class="wp-block-preformatted">\definecolor{darkblue}{rgb}{0.0, 0.0, 0.55}
\definecolor{darkgray}{rgb}{0.66, 0.66, 0.66}</pre>
<p>Below example demonstrate this a table with alternate colours, \rowcolors take the following options <em>\rowcolors{row_start_colour}{even_row_colour}{odd_row_colour}</em>.</p>
<pre class="wp-block-preformatted">\rowcolors{2}{darkgray}{gray!20}
\begin{tabular}{c|c} Release &amp;;Codename \\ \hline Fedora Core 1 &amp;;Yarrow \\ Fedora Core 2 &amp;;Tettnang \\ Fedora Core 3 &amp;;Heidelberg \\ Fedora Core 4 &amp;;Stentz \\
\end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-7.png" alt="" /></figure>
<p>In addition to the above example, \rowcolor can be used to specify the colour of each row, this method works best when there are multi-rows. The following examples show the impact of using the \rowcolours with multi-row and how to work around it.</p>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-8.png" alt="" /></figure>
<p>As you can see the <em>multi-row</em> is visible in the first row, to fix this we have to do the following.</p>
<pre class="wp-block-preformatted">\begin{tabular}{|l|l|}\hline \rowcolor{darkblue}\textsc{\color{white}Release} &amp;;\textsc{\color{white}Codename} \\ \hline \rowcolor{gray!10}Fedora Core 4 &amp;;Stentz \\ \hline \rowcolor{gray!40}&amp;;Fedora 8 \\ \rowcolor{gray!40}\multirow{-2}{*}{Multi-Row} &amp;;Werewolf \\ \hline
\end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-9.png" alt="" /></figure>
<p>Let us discuss the changes that were implemented to resolve the multi-row with the alternate colour issue.</p>
<ul>
<li>The first row started above the multi-row</li>
<li>The number of rows was changed from 2 to -2, which means to read from the line above</li>
<li>\rowcolor was specified for each row, more importantly, the multi-rows must have the same colour so that you can have the desired results.</li>
</ul>
<p>One last note on colour, to change the colour of a column you need to create a new column type and define the colour. The example below illustrates how to define the new column colour.</p>
<pre class="wp-block-preformatted">\newcolumntype{g}{&gt;{\columncolor{darkblue}}l} </pre>
<p>Let’s break it down:</p>
<ul>
<li>\newcolumntype{g}: defines the letter <em>g</em> as the new column</li>
<li>{&gt;{\columncolor{darkblue}}l}: here we select our desired colour, and <em>l</em> tells the column to be left-justified, this can be subsitued with <em>c</em> or <em>r</em></li>
</ul>
<pre class="wp-block-preformatted">\begin{tabular}{g|l} \textsc{Release} &amp;;\textsc{Codename} \\ \hline Fedora Core 4 &amp;;Stentz \\ &amp;Fedora 8 \\ \multirow{-2}{*}{Multi-Row} &amp;;Werewolf \\ \end{tabular}\</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-10.png" alt="" /></figure>
<h2>Landscape table</h2>
<p>There may be times when your table has many columns and will not fit elegantly in portrait. With the <em>rotating</em> package in preamble you will be able to create a sideways table. The below example demonstrates this.</p>
<p>For the landscape table, we will use the <em>sidewaystable</em> environment and add the tabular environment within it, we also specified additional options.</p>
<ul>
<li>\centering to position the table in the centre of the page</li>
<li>\caption{} to give our table a name</li>
<li>\label{} this enables us to reference the table in our document</li>
</ul>
<pre class="wp-block-preformatted">\begin{sidewaystable}
\centering
\caption{Sideways Table}
\label{sidetable}
\begin{tabular}{ll} \rowcolor{darkblue}\textsc{\color{white}Release} &amp;;\textsc{\color{white}Codename} \\ \rowcolor{gray!10}Fedora Core 4 &amp;;Stentz \\ \rowcolor{gray!40} &amp;;Fedora 8 \\ \rowcolor{gray!40}\multirow{-2}{*}{Multi-Row} &amp;;Werewolf \\ \end{tabular}\vspace{3mm}
\end{sidewaystable}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-11.png" alt="" /></figure>
<h2>Lists in tables</h2>
<p>To include a list into a table you can use tabularx and include the list in the column where the <em>X</em> is specified. Another option will be to use tabular but you must specify the column width.</p>
<h3>List in tabularx</h3>
<pre class="wp-block-preformatted">\begin{tabularx}{\textwidth}{|l|X|} \hline Fedora Version &amp;;Editions \\ \hline Fedora 32 &amp;;\begin{itemize}[noitemsep] \item CoreOS \item Silverblue \item IoT \end{itemize} \\ \hline
\end{tabularx}\vspace{3mm}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-12.png" alt="" /></figure>
<h3>List in tabular</h3>
<pre class="wp-block-preformatted">\begin{tabular}{|l|m{6cm}|}\hline Fedora Version &amp;;Editions \\ \hline Fedora 32 &amp;;\begin{itemize}[noitemsep] \item CoreOS \item Silverblue \item IoT \end{itemize} \\ \hline
\end{tabular}</pre>
<figure class="wp-block-image size-large"><img src="https://www.sickgaming.net/blog/wp-content/uploads/2020/06/latex-typesetting-part-2-tables-13.png" alt="" /></figure>
<h2>Conclusion</h2>
<p>LaTeX offers many ways to customise your table with tabular and tabularx, you can also add both tabular and tabularx within the table environment (\begin\table) to add the table name and to position the table.</p>
<p>The packages used in this series are:</p>
<pre class="wp-block-preformatted">\usepackage{fullpage}
\usepackage{blindtext} % add demo text
\usepackage{array} % used for column positions
\usepackage{tabularx} % adds tabularx which is used for text wrapping
\usepackage{multirow} % multi-row and multi-colour support
\usepackage[table]{xcolor} % add colour to the columns \usepackage{rotating} % for landscape/sideways tables</pre>
<h2>Additional Reading</h2>
<p>This was an intermediate lesson on tables. For more advanced information about tables and LaTex in general, you can go to the <a href="https://en.wikibooks.org/wiki/LaTeX/Tables">LaTeX Wiki</a>.</p></p>
</div>


https://www.sickgaming.net/blog/2020/06/...-2-tables/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016