Tabulate supports both automatic and manual word-wrapping to control how text breaks within table cells. This ensures your content fits neatly within defined cell widths.
You can override automatic wrapping by embedding newline characters (\n) in your cell content:
table.add_row({"This paragraph \nhas embedded '\\n' \ncharacters and\n will break\n exactly where\n you want it\n to\n break."});
Automatic word-wrapping is used only if the cell contents do not have any embedded newline \n characters. If newlines are present, they take precedence.
Here’s an example demonstrating both automatic and manual word wrapping:
#include <tabulate/table.hpp>using namespace tabulate;int main() { Table table; table.add_row({"This paragraph contains a veryveryveryveryveryverylong word. The long word will " "break and word wrap to the next line.", "This paragraph \nhas embedded '\\n' \ncharacters and\n will break\n exactly " "where\n you want it\n to\n break."}); table[0][0].format().width(20); table[0][1].format().width(50); std::cout << table << std::endl;}
The first cell has a width of 20 characters. The long word “veryveryveryveryveryverylong” automatically breaks and wraps to fit within the cell width.
2
Second column (manual)
The second cell has a width of 50 characters, which provides plenty of space. However, the embedded \n characters force line breaks at specific positions, regardless of available space.
Regardless of whether word-wrapping is automatic or manual, Tabulate performs a trim operation on each line of each cell to remove whitespace characters from either side.
This ensures consistent formatting and eliminates unintended spacing:
// These are equivalent after trimming:table.add_row({" Text with spaces "});table.add_row({"Text with spaces"});
For longer text content, set an appropriate width and let automatic wrapping do the work:
table.add_row({"This is a longer paragraph that describes something in detail. " "It will automatically wrap to multiple lines based on the cell width."});table[0][0].format().width(40);
Combine word wrapping with text alignment for professional-looking tables. For example, use left alignment for text-heavy columns and right alignment for narrow numeric columns.
Word wrapping works seamlessly with text alignment:
table[0][0].format() .width(30) .font_align(FontAlign::center); // Wrapped text will be centeredtable[0][1].format() .width(40) .font_align(FontAlign::right); // Wrapped text will be right-aligned
Each wrapped line respects the alignment setting, creating visually consistent multi-line cells.