Thursday, October 30, 2014

Latex: Underlining text along with adjacent white space

To underline text in latex we use \underline{...}.
In cases when we need to underline text and the adjoining whitespace (before and after the text), like in situations of having a filled form, we can use the \makebox command as below:

\underline{\makebox[2.5in][l]{Jane Doe}}

Reference:
1. http://www.latex-community.org/forum/viewtopic.php?f=44&t=5162

Saturday, October 11, 2014

Latex: Centering text in a table row when there is a image

When you are embedding images in a Latex table, often the text gets aligned to the bottom of the row, and the image decides the height of the row. To align the text in the columns to be vertically centered we have two options, as discussed in references [1][2][3][4].

Solution 1

Vertically centering cell entries is possible via the m{<width>} column type from the array package. Horizontal centering is obtained by prepending the column entries with \centering\arraybackslash (also supported by array). As described in [2], you can defines a new column type M which does all of the above:
\usepackage{array}
\newcolumntype{M}{>{\centering\arraybackslash}m{\dimexpr.25\linewidth-2\tabcolsep}}
And use can use it in a table as:
\begin{tabular}{|M|M|M|M|}

Or as described in [4], we can also define a new column type C as follows:
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\arraybackslash}m{#1}}

Solution 2

Another simpler solution is to raise the baseline of the image, as text in a row is aligned to the image's baseline. The baseline of an image is at the bottom. You could use \raisebox to shift it up. Use half of \height, which stands for the height of the box to be raised.
\raisebox{-.5\height}{\includegraphics{some_picture}}

You can use it in a table as follows:
\begin{tabular}{lll}
\raisebox{-.5\height}{\includegraphics[scale=0.25]{example-image}} & text & text\\
\end{tabular}

References:
1. http://stackoverflow.com/questions/1357798/how-to-center-cell-contents-of-a-latex-table-whose-columns-have-fixed-widths
2. http://tex.stackexchange.com/questions/46386/vertically-center-cells-of-a-table
3. http://tex.stackexchange.com/questions/19080/how-to-vertically-center-text-with-an-image-in-the-same-row-of-a-table
4. http://www.latex-community.org/forum/viewtopic.php?f=45&t=15908

Latex: Image in a table overlaps the row separator line

When embedding images in Latex tables, you may find that the images will overlap with the horizontal line above its row. Like described by Stefan Kottwitz in [1], it looks like:

To avoid this problem, the easiest solution is to use the "trim" parameter of the includegraphics command. The trim option takes four lengths as argument, specifying the amount to remove or add to each side. trim= 1 2 3 4 would "crop" the picture by 1bp at the left, 2bp at the bottom, 3bp on the right and 4bp at the top.

To add some spacing at the top, we can use a negative value as shown below:
\includegraphics[trim=0 0 0 -5]{figure.jpg}

In case multiple parameters are to be specified, such as "width", we can do it as follows:
\includegraphics[trim=0 0 0 -5,width=0.2\textwidth]{figure.jpg}

References:
1. http://tex.stackexchange.com/questions/41788/image-in-table-covers-horizontal-line-above-it