# Set the width of table columns

Changes made to table column width settings will affect the *typeset table* only, not the *preview* provided by **Visual Editor**. You need to recompile your document to see the result of any changes made to column widths.

Follow these steps to set column width(s) to a fixed value, or allow the width(s) to vary according to the content in the column cells.

1. [Select the column(s)](#how-to-select-one-or-more-columns-or-rows) whose width you want to change. The column width icon ( <img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FgVrpe08ixUad7m7U5V40%2FInsertTableSetColumnWidth.png?alt=media&#x26;token=23f34363-c4a2-4e05-ac71-80a3586a7595" alt="" data-size="line">) is then enabled.
2. Select the drop-down menu:

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F83t1nTpZ2JEfuNfLrU1m%2FTableColumnWidthTypes.png?alt=media&#x26;token=b269bf57-9ad8-43ac-b05b-c957548d9981" alt="Drop-down menu for width of columns"><figcaption><p>Menu options showing the width options</p></figcaption></figure>

3. Choose the preferred option for the selected column(s):
   * **Stretch width to text:** this option allows the typeset column width(s) to be determined by the amount of content contained in the column's cells, e.g., the width of typeset text or mathematics.
   * **Fixed width, wrap text**: sets the width of the column(s) to a fixed value which causes line breaking (text wrapping) within column cells. Selecting this option displays the Set column width window used to set fixed column width(s), as explained in the next section.

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FDaK5MI8Z2BzrEVTfVkNU%2FSetTableColumnWidth.png?alt=media&#x26;token=4cae195b-47ad-4477-b016-34588adb1dfc" alt="Modal window used to set a fixed column width" width="375"><figcaption></figcaption></figure>

## How to set a fixed column width

The **Set column width** pop-up window provides a drop-down list containing three ways to set fixed column widths:

* As a percentage (%) of the current environment's line width: the table column widths might then vary according to where the table is used (see [example below](#example-using-percentage-of-line-width)).
* Using explicit widths expressed in units of millimeters (mm), centimeters (cm), [inches](https://en.wikipedia.org/wiki/Inch) (in) or [points](https://en.wikipedia.org/wiki/Point_\(typography\)) (pt).
* A **Custom** option using LaTeX commands to calculate the width ([see example below](#example-using-custom-commands)).

The following image shows how **Visual Editor** uses a small icon to indicate columns with a fixed width. You can edit the fixed-width setting by selecting (clicking) the icon to display the **Set column width** window.

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FgtEKjXsmyahtJh8XiZBY%2FVisualEditorFixedWidth2.png?alt=media&#x26;token=be12d5cc-359d-4437-bfaf-79c271cb3037" alt="Image showing how Visual Editor highlights fixed width columns" width="563"><figcaption></figcaption></figure>

### Example: using percentage of line width

The LaTeX command `\linewidth` holds the value of the current line width, which varies based on the document format, such as single-column or double-column layouts. Additionally, the value of `\linewidth` can change in specific environments, e.g., within a `minipage`.

This example uses the `multicol` package to typeset two identical tables with column widths set to `0.25\linewidth`. One table is typeset in a single-column section, the other in a two-column section, demonstrating how changes to `\linewidth` affects the table column widths.

```latex
\documentclass{article}
\usepackage{array}
\usepackage{multicol}
\begin{document}
\section{Section with a single column}
This part of the page is a single column with \verb|\linewidth|=\the\linewidth.\vskip12pt 

\begin{tabular}{|p{0.25\linewidth}|p{0.25\linewidth}|}
\hline
A & B\\
\hline
C & D \\
\hline
\end{tabular}

\vskip12pt \noindent We now typeset the same table in a two-column section.
\begin{multicols}{2}
[
\section{Section with two columns}
]
Here, \verb|\linewidth|=\the\linewidth. The table column widths are smaller.

\begin{tabular}{|p{0.25\linewidth}|p{0.25\linewidth}|}
\hline
A & B\\
\hline
C & D \\
\hline
\end{tabular}
\end{multicols}
\end{document}
```

[ Open this example in Overleaf and switch to Visual Editor.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+line+width+to+set+table+column+widths\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Barray%7D%0A%5Cusepackage%7Bmulticol%7D%0A%5Cbegin%7Bdocument%7D%0A%5Csection%7BSection+with+a+single+column%7D%0AThis+part+of+the+page+is+a+single+column+with+%5Cverb%7C%5Clinewidth%7C%3D%5Cthe%5Clinewidth.%5Cvskip12pt+%0A%0A%5Cbegin%7Btabular%7D%7B%7Cp%7B0.25%5Clinewidth%7D%7Cp%7B0.25%5Clinewidth%7D%7C%7D%0A%5Chline%0AA+%26+B%5C%5C%0A%5Chline%0AC+%26+D+%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%0A%0A%5Cvskip12pt+%5Cnoindent+We+now+typeset+the+same+table+in+a+two-column+section.%0A%5Cbegin%7Bmulticols%7D%7B2%7D%0A%5B%0A%5Csection%7BSection+with+two+columns%7D%0A%5D%0AHere%2C+%5Cverb%7C%5Clinewidth%7C%3D%5Cthe%5Clinewidth.+The+table+column+widths+are+smaller.%0A%0A%5Cbegin%7Btabular%7D%7B%7Cp%7B0.25%5Clinewidth%7D%7Cp%7B0.25%5Clinewidth%7D%7C%7D%0A%5Chline%0AA+%26+B%5C%5C%0A%5Chline%0AC+%26+D+%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Bmulticols%7D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Output from the code above](https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FnViPiuXlxqPwIJn0nSIk%2FTableColumnLinewidth%20\(1\).svg?alt=media\&token=c1719474-2b44-4200-9983-a20dd6aa125a)

### Example: using custom commands

The **Custom** option lets you set column widths using a wider range of units including [`em`, `ex` and `sp`](https://learn.overleaf.com/learn/Lengths_in_LaTeX) together with LaTeX commands—provided those commands produce a valid width value in units LaTeX understands.

The next example demonstrates just a few of the many ways you can use the **Custom** option to set column widths, including use of the units `em` and `ex`, simple arithmetic expressions and more advanced calculations using LaTeX commands. The`\dimexpr` ("dimension expression") command is used to perform arithmetic operations on LaTeX lengths (also called dimensions) using addition, subtraction, multiplication, or division. In essence, `\dimexpr` enables use of dynamically-calculated lengths, including the width of table columns.

```latex
\documentclass{article}
\usepackage{array}
\begin{document}
\newdimen\mydim
\mydim=1.25cm
\newcommand{\Acol}{\dimexpr 1cm + 0.1in + 3pt + 2.5mm\relax}
\newcommand{\Bcol}{0.1\linewidth}

\begin{itemize}
    \item Width of column 1: 4em=\the\dimexpr 4em\relax
    \item Width of column 2: 3ex=\the\dimexpr 3ex\relax
    \item Width of column 3: \verb|\Acol| sets a width of \the\dimexpr 1cm + 0.1in + 3pt + 2.5mm\relax
    \item Width of column 4: \verb|\Bcol| sets a width of \the\dimexpr0.1\linewidth\relax
    \item Width of column 5: \verb|\dimexpr0.25\linewidth+2cm-\mydim\relax| sets a width of \the\dimexpr0.2\linewidth+2cm-\mydim\relax
\end{itemize}

\begin{table}
\centering
\caption{Using \LaTeX{} commands and units to set column widths}
\begin{tabular}{|p{4em}|p{3ex}|p{\Acol}|p{\Bcol}|p{\dimexpr0.2\linewidth+2cm-\mydim\relax}|}
\hline
A & B &C & D&E\\
\hline
F& G&H& I&J\\
\hline
\end{tabular}
\end{table}
\end{document}
```

[Open this example in Overleaf and switch to Visual Editor.](https://www.overleaf.com/docs?engine=pdflatex\&snip_name=Using+LaTeX+commands+to+set+table+column+widths\&snip=%5Cdocumentclass%7Barticle%7D%0A%5Cusepackage%7Barray%7D%0A%5Cbegin%7Bdocument%7D%0A%5Cnewdimen%5Cmydim%0A%5Cmydim%3D1.25cm%0A%5Cnewcommand%7B%5CAcol%7D%7B%5Cdimexpr+1cm+%2B+0.1in+%2B+3pt+%2B+2.5mm%5Crelax%7D%0A%5Cnewcommand%7B%5CBcol%7D%7B0.1%5Clinewidth%7D%0A%0A%5Cbegin%7Bitemize%7D%0A++++%5Citem+Width+of+column+1%3A+4em%3D%5Cthe%5Cdimexpr+4em%5Crelax%0A++++%5Citem+Width+of+column+2%3A+3ex%3D%5Cthe%5Cdimexpr+3ex%5Crelax%0A++++%5Citem+Width+of+column+3%3A+%5Cverb%7C%5CAcol%7C+sets+a+width+of+%5Cthe%5Cdimexpr+1cm+%2B+0.1in+%2B+3pt+%2B+2.5mm%5Crelax%0A++++%5Citem+Width+of+column+4%3A+%5Cverb%7C%5CBcol%7C+sets+a+width+of+%5Cthe%5Cdimexpr0.1%5Clinewidth%5Crelax%0A++++%5Citem+Width+of+column+5%3A+%5Cverb%7C%5Cdimexpr0.25%5Clinewidth%2B2cm-%5Cmydim%5Crelax%7C+sets+a+width+of+%5Cthe%5Cdimexpr0.2%5Clinewidth%2B2cm-%5Cmydim%5Crelax%0A%5Cend%7Bitemize%7D%0A%0A%5Cbegin%7Btable%7D%0A%5Ccentering%0A%5Ccaption%7BUsing+%5CLaTeX%7B%7D+commands+and+units+to+set+column+widths%7D%0A%5Cbegin%7Btabular%7D%7B%7Cp%7B4em%7D%7Cp%7B3ex%7D%7Cp%7B%5CAcol%7D%7Cp%7B%5CBcol%7D%7Cp%7B%5Cdimexpr0.2%5Clinewidth%2B2cm-%5Cmydim%5Crelax%7D%7C%7D%0A%5Chline%0AA+%26+B+%26C+%26+D%26E%5C%5C%0A%5Chline%0AF%26+G%26H%26+I%26J%5C%5C%0A%5Chline%0A%5Cend%7Btabular%7D%0A%5Cend%7Btable%7D%0A%5Cend%7Bdocument%7D)

This example produces the following output:

![Output from the code above using LaTeX commands to set table column widths](https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FAce9Z1pulKewUJjLeCTJ%2FTableColumnCustom.svg?alt=media\&token=68d61e35-b5c2-4e79-8f70-e7c2f24c9fc8)

## How to change the alignment of a table cell

* The alignment of individual, *non-merged*, cells can’t be changed in **Visual Editor**.
* The content of *merged* cells can be aligned (see the section [Merging two or more cells in a table row](#merging-two-or-more-cells-in-a-table-row)).

## How to merge (or unmerge) table cells

Table cells can be merged horizontally, within a table *row*, but not vertically within a table column.

### Merging two or more cells in a table row

1. Select the cells you want to merge.
2. Select the **Merge cells** icon (<img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F7dOUREWw37dABt8uUCj0%2FInsertTableMenuMerge.png?alt=media&#x26;token=8f40226f-92c8-4b31-b681-ae91ad840166" alt="" data-size="line">) to merge the cells.
3. Optionally, you can change the alignment of the merged cells.

### Unmerging two or more merged cells

1. Select the merged cell.
2. Select the **Merge cells** icon (<img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F7dOUREWw37dABt8uUCj0%2FInsertTableMenuMerge.png?alt=media&#x26;token=8f40226f-92c8-4b31-b681-ae91ad840166" alt="" data-size="line">) to unmerge the cells.

The following video demonstrates merging three cells in a table row, changing alignment of the merged cell, and unmerging the cell content.

{% embed url="<https://videos.ctfassets.net/nrgyaltdicpt/2g1bZr5H5PdCBdrD6MqXPN/7813a6c42b2ecbd4ac7cbb4a412b863c/MergingCells.mp4>" %}

{% hint style="info" %}
All content contained in the merged cell is placed in the first cell of the unmerged set.
{% endhint %}

## How to delete a table row or column

1. [Select the rows or columns](#how-to-select-one-or-more-columns-or-rows) you want to delete.
2. Select the **Delete row or column** icon (<img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F7KCA2AvbcJXVSlxKIoA9%2FInsertTableMenuDelRowCol.png?alt=media&#x26;token=9e0f4daf-7b07-43a7-9726-b28827baa664" alt="" data-size="line">) to delete the selected rows or columns.

## How to add new rows or columns

The **Insert** button (<img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F5GpVtSqNkUjroEgMDyf2%2FInsertTableMenuInsRowCol.png?alt=media&#x26;token=f571f61a-926d-4c6c-b482-87b37ea3203b" alt="" data-size="line">) lets you add one or more new rows or columns before or after a selected location.

If you want to insert *m* rows and *n* columns, select an *m* × *n* group of cells covering the locations where you want to insert those rows and columns.

**Example**: In the following screenshot, a 3 × 2 group of cells straddling 3 rows and 2 columns has been selected.&#x20;

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FfMT5trCwDaPMkAOEUGi8%2FInsertTableRowColInsert.png?alt=media&#x26;token=c908b67b-11ee-4497-a8c5-50da00db1569" alt="Selecting a group of cells"><figcaption><p>""</p></figcaption></figure>

* Selecting <img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2F5GpVtSqNkUjroEgMDyf2%2FInsertTableMenuInsRowCol.png?alt=media&#x26;token=f571f61a-926d-4c6c-b482-87b37ea3203b" alt="" data-size="line"> provides the following options:

<figure><img src="https://3502988919-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVetOkhFZmAC8QCQK0Pi7%2Fuploads%2FNWlDG2WgUrS2wuGQcuPR%2FInsertTableoptionsDisplayed.png?alt=media&#x26;token=1e504836-e352-41c6-8c36-da619b3d54f0" alt="Options provided when selecting the +"><figcaption><p>""</p></figcaption></figure>

* You can now insert any of the following:
  * 2 columns to the left of column 1
  * 2 columns to the right of column 2
  * 3 rows above row 1
  * 3 rows below row 3
