1

This is my code:

\documentclass[a4paper, 12pt]{report}

\usepackage{adjustbox}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{tabularray}
\usepackage{color, xcolor}
\usepackage{amsmath, amsfonts, amssymb, amsthm}
\usepackage{array}
\usepackage{floatrow}

\begin{document}

\begin{table}[H]
    \centering
    \begin{adjustbox}{width=\textwidth}
        \begin{tabular}{|p{195pt}|p{195pt}|}
            \hline
            \textbf{Modul} & \textbf{Description} \\ \hline
            \texttt{python.package.name.submodul.class.method} & Test\\ \hline
        \end{tabular}
    \end{adjustbox} 
\end{table}

\end{document}

This is the poor output without linebreak:

enter image description here

Here is the code with just text:

\documentclass[a4paper, 12pt]{report}

\usepackage{adjustbox}
\usepackage{makecell}
\usepackage{booktabs}
\usepackage{tabularray}
\usepackage{color, xcolor}
\usepackage{amsmath, amsfonts, amssymb, amsthm}
\usepackage{array}
\usepackage{floatrow}

\begin{document}

\begin{table}[H]
    \centering
    \begin{adjustbox}{width=\textwidth}
        \begin{tabular}{|p{195pt}|p{195pt}|}
            \hline
            \textbf{Modul} & \textbf{Description} \\ \hline
            \texttt{This is a very very long sentence. How can I do a line break here? It doesn't work with package names in Python.} & Test\\ \hline
        \end{tabular}
    \end{adjustbox} 
\end{table}

\end{document}

For some reason this one does a linebreak:

enter image description here

Why does LaTeX not break long technical names, such as Python package names, at dots inside \texttt{…} in a table cell, and how can I automatically enable such line breaks (e.g., using \allowbreak or \discretionary)?

Do you have an idea?

1
  • 1
    This was flagged as duplicate, but the only effect of this decision is to make it impossible to contribute lightweight solution addressing the specificities of this question, which is more precise than the linked-to one. Commented Aug 13 at 8:52

1 Answer 1

7

In your particular case I'd say that only the . is a viable line break for Python modules, so you could also use (in extension to the answers of the suspected duplicate -- this just automatically adds \allowbreak in front of each . in its arguments):

\documentclass[a4paper, 12pt]{report}

\usepackage{tabularx}
\usepackage{array}

\ExplSyntaxOn
\NewDocumentCommand \pymod { m }
  {
    \texttt
      {
        \tl_set:Nn \l_tmpa_tl {#1}
        \tl_replace_all:Nnn \l_tmpa_tl { . } { \allowbreak . }
        \tl_use:N \l_tmpa_tl
      }
  }
\ExplSyntaxOff

\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|X|X|}
  \hline
  \textbf{Modul} & \textbf{Description} \\ \hline
  \pymod{python.package.name.submodul.class.method} & Test\\ \hline
\end{tabularx}
\end{document}

enter image description here

Asides:

  • Don't resize tables using \adjustbox or \scalebox or anything like that! It leads to inconsistent font sizes and rule thicknesses and just looks ugly, better use tabularx and X columns than fixed width p columns that you then scale.
  • You should really consider whether you want to use H placement. "Floats" placed that way can appear out of order if you use normal floats around them, so as soon as you use a H float you have to manually check every other float.
  • Your table would look prettier if you omitted all the vertical rules and used booktabs rules for the horizontal rules (just a \toprule and \midrule around the first two rows, and a \bottomrule at the end, no rules in between).

There is one thing that is exclusive to tables which really makes a difference compared to the suspected duplicate: Inside a table you can use a custom column type. We can use that to automatically insert \pymod around each cell in the table.

In this I also change the used \pymod macro to have an optional argument to specify what to insert in front of the periods in case there is a linebreak. To get that behaviour I changed the \allowbreaks to \discretionarys. We can then use \kern1em for our table columns. This will have the effect that every following element will appear to be indented by 1em. I also use booktabs rules to get a prettier result.

\documentclass[a4paper, 12pt]{report}

\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{array}
\usepackage{collcell}

\ExplSyntaxOn
\NewDocumentCommand \pymod { O{} m }
  {
    \texttt
      {
        \tl_set:Nn \l_tmpa_tl {#2}
        \tl_replace_all:Nnn \l_tmpa_tl { . } { \discretionary{}{#1}{} . }
        \tl_use:N \l_tmpa_tl
      }
  }
\ExplSyntaxOff

\newcolumntype\pymod[1]
  {>{\collectcell{\pymod[\kern1em]}}#1<{\endcollectcell}}

\begin{document}
% showing the behaviour in normal text without trailing indentation.
\begin{minipage}{5cm}
  \pymod{python.package.name.submodul.class.method}
\end{minipage}

\bigskip
\noindent
\begin{tabularx}{\linewidth}{\pymod{X}X}
  \toprule
  % use \multicolumn so that this one cell isn't affected by \pymod
  \multicolumn{1}{X}{\textbf{Modul}} & \textbf{Description} \\
  \midrule
  python.package.name.submodul.class.method & Some really cool Python module
  that is supper handy. In particular this provides a method of a class of a
  submodule. \\
  \addlinespace
  tex.sx.answer.skillmon.smart & Smart answers are just smart, and make tables
  look nicer with less user input \\
  \bottomrule
\end{tabularx}
\end{document}

enter image description here

2
  • Something is wrong. If I try to run exactly your code, I will get following error: Package array Error: Illegal pream-token (\pymod): `c' used. \end{tabularx}. What does that mean? Commented Sep 12 at 8:30
  • 1
    @user2379123 it means that you're not running exactly my code. The \newcolumntype\pymod[1]{>{\collectcell{\pymod[\kern1em]}}#1<{\endcollectcell}} isn't executed. Commented Sep 12 at 9:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.