|
| 1 | +--- |
| 2 | +title: "Evaluating table layouts and formatting" |
| 3 | +image: |
| 4 | + path: /images/so-simple-sample-image-4.jpg |
| 5 | + thumbnail: /images/site-logo.png |
| 6 | +--- |
| 7 | + |
| 8 | +One of the most helpful tools when creating tables for Markdown or RST is the Tables Generator at https://www.tablesgenerator.com/markdown_tables. You can draw tables or paste table data and then render the ASCII-based output for pasting into your document source file. |
| 9 | + |
| 10 | +For example, here is an empty five-column table in Markdown, ready for you to insert cell data and spaces. |
| 11 | + |
| 12 | +``` |
| 13 | +| | | | | | |
| 14 | +|:-|:-|:-|:-|:-| |
| 15 | +| | | | | | |
| 16 | +| | | | | | |
| 17 | +| | | | | | |
| 18 | +``` |
| 19 | + |
| 20 | +When using Markdown for tables, you do not have access to block-level formatting. If you need a second paragraph, you can use `<br>` inside of a Markdown table. For more complex tables, many people use HTML inside of Markdown files. |
| 21 | + |
| 22 | +For RST, there are several options for table markup that is super simple while still allowing for nice table output. |
| 23 | + |
| 24 | +Simple tables can be made with dashes and plus signs and pipe symbols. Use spaces to indicate the size of cells. However these can be difficult to hand-type and maintain with changes over time. So, look for other table syntax ideas to make it easier to maintain the tables. |
| 25 | + |
| 26 | +The [RST documentation](https://thomas-cokelaer.info/tutorials/sphinx/rest_syntax.html#tables) has several table syntax examples. Over time I have found that the most useful way to maintain table data in columns and rows is the `csv-table` directive. Indicate header rows and labels in the cells, along with the widths of each column. Then enter the data in a CSV-like stye. Here's an example: |
| 27 | + |
| 28 | +``` |
| 29 | +.. csv-table:: a title |
| 30 | + :header: "name", "firstname", "age" |
| 31 | + :widths: 20, 20, 10 |
| 32 | +
|
| 33 | + "Smith", "John", 40 |
| 34 | + "Smith", "John, Junior", 20 |
| 35 | +``` |
| 36 | + |
| 37 | +If you also output PDF using LaTeX with Sphinx, be aware that there's a column specification for tabular data. Then you can use centimeters for width like so: |
| 38 | + |
| 39 | +``` |
| 40 | +.. tabularcolumns:: |l|c|p{5cm}| |
| 41 | ++--------------+---+-----------+ |
| 42 | +| simple text | 2 | 3 | |
| 43 | ++--------------+---+-----------+ |
| 44 | +``` |
| 45 | + |
| 46 | +Your editor may also have helpful tools for managing tables, such as the [Markdown Table Editor in Atom](https://atom.io/packages/markdown-table-editor), which can resize all rows and columns while you type. Plus, it enables keybindings that you can use to navigate between cells and rows. |
0 commit comments