How to have wrapped text when exporting from org to Latex?

I have an org file which I export to LaTeX and then to a PDF document. The problem is I have some long text that exceeds the length of the page. How to wrap it so that it falls into the next line?

First example:

#+BEGIN_SRC c++
<code goes here> // very long comment that doesn't wrap ........
#+END_SRC


The comment is very long and exceeds the length of the page, how to make it wrap?

Second example:

|------+------+------------------------+------|
| text | text | text                   | text |
|------+------+------------------------+------|
| text | text | very long texttt...... | text |
|------+------+------------------------+------|


Some cells contain long text which also doesn't wrap, how to make it wrap?

From a LaTeX point of view, these are different cases.

In the first case, LaTeX isn't going to wrap -- and it shouldn't! A "verbatim" environment, which is what source-code is set in, respects lines precisely and doesn't break paragraphs, because LaTeX simply can't know where to break them. If you want to wrap the lines, you have to wrap them in the source-code itself.

In the second case, you can use #+ATTR_LATEX to help LaTeX format the table appropriately.

Simplest but least elegant approach: use :align with a p column to specify a width for the problematic column. LaTeX will then wrap that column to the specified width. So this specifies three columns: one aligned left, one a p column with a fixed width of 4cm, and another aligned left.

#+ATTR_LATEX: :align lp{4cm}l


More elegant. Use tabularx and a X column, and LaTeX will then set the column to a length appropriate to enable a table of a fixed overall width. You need to (add-to-list 'org-latex-packages-alist '("" "tabularx")) I think.

#+ATTR_LATEX: :environment tabularx :width \textwidth :align lXl
| column | column with very very very very very overlong text which would flow over | last   |
| column | shorter column                                                           | column |
| column | shorter column                                                           | column |


• you can also use #+LATEX_HEADER: \usepackage{tabularx} if you only want to add the tabularx package to the local document Jun 19 at 17:18