T-F-S / tcolorbox

A LaTeX package to create highly customizable colored boxes.
http://www.ctan.org/pkg/tcolorbox
LaTeX Project Public License v1.3c
213 stars 15 forks source link

How to linebreak on NewTotalTCBox? #272

Closed Heziode closed 4 months ago

Heziode commented 4 months ago

Hi 👋 ,

I am trying to find a way to have a linebreak using \NewTotalTCBox in order to prevent line overflow.

The goal is to have an inline code, but, that can be displayed on several lines if it too large.

Example:

Example of inline tcolorbox overflow

\documentclass[12pt]{article}
\usepackage{minted}
\usepackage[all]{tcolorbox}

\setmintedinline{breaklines,breakanywhere=true} % necessary for breakanywhere to work later on.

\DeclareTotalTCBox{\CodeInline}{ O{text} v }{%
    verbatim,
    breakable, % Allow for line breaks
    force nobeforeafter, 
    enlarge top initially by=0mm,
    enlarge bottom finally by=0mm,
    colback=gray!10,
    coltext=black,
    colframe=gray!10,
    boxrule=0.25mm,
    arc=3pt,
    listing engine=minted,
    listing only,
}
{\mintinline{#1}{#2}}

\begin{document}
Eum eligendi veritatis est omnis beatae velit est ipsam explicabo quis quia animi. Reiciendis dolor modi omnis ipsum adipisci quo ducimus. Dolores sequi mollitia est deserunt quia doloribus fugit ut pariatur voluptas ut perspiciatis iure. Maiores qui nulla est aut modi. Unde occaecati incidunt dolor velit dolorum voluptas possimus excepturi voluptatum. Dolores qui numquam ipsa quia totam animi explicabo quisquam qui iste illum esse ipsam labore odit. Impedit quo nisi labore voluptatem \CodeInline{/path/to/file.ext:123:4:TYPE/path/to/file.ext:123:4:TYPE} aut deserunt dolorum quidem. Omnis dolorum sapiente et occaecati labore dicta fuga. Laborum reiciendis consequatur rerum ex molestias voluptates autem et. Magnam numquam minima illo magnam. \mintinline{text}{/path/to/file.ext:123:4:TYPE/path/to/file.ext:123:4:TYPE} Quisquam quos eos in non ullam aut aperiam. Voluptas quo quia dolores quis ipsa velit odio consequuntur laboriosam cum. Illo pariatur molestiae tempora voluptatibus autem cum pariatur aperiam ipsa ipsa ut labore. Ut temporibus voluptatibus quos architecto perspiciatis. Quia non porro quaerat maiores vero saepe facere aut occaecati consequatur est et voluptate quasi. Qui sunt dolor deleniti aut qui.
\end{document}

As we can see, \mintinline works well (break line anywhere) when it's not inside a tcolorbox, but does not work anymore when it is inside a TotalTCBox.

muzimuzhi commented 4 months ago

Your example doesn't reproduce your screenshot. Some font setting and perhaps minted setting are missing.

muzimuzhi commented 4 months ago
\DeclareTotalTCBox{\CodeInline}{ O{text} v }{%
    verbatim,
    breakable, % Allow for line breaks
    ...
}

First, the /tcb/breakable option only enables page breaking in between tcolorboxes, and has nothing to do with line breaking in the middle of inline boxes created by \tcbox and derived commands, thus also the boxes defined with \NewTotalTCBox.

That's said, total boxes defined with \NewTotalTCBox cannot break into lines.

Decoration of arbitrary text (e.g., text consists of verbatim) which may span multiple lines and even multiple pages is hard in classic (non-LuaTeX) TeX engines. The general idea is to get positions and/or geometries of each broken text line and then add decorations to each of them. The decoration ranges from simple \rule to complex tikzpicture and even \tcbox.

Heziode commented 4 months ago

Your example doesn't reproduce your screenshot. Some font setting and perhaps minted setting are missing.

I updated the code to have a MWE. Only the font differ, but I also tested like this with the default font, and the problem is still here.


Thank you for the clarification. Since I use lualatex, I will take a look at lua-ul.

I close the issue, since a detailed answer has been given.

Heziode commented 4 months ago

Here is my solution, based on the post:

\documentclass[12pt]{article}
\usepackage{minted}
\usepackage{iftex} % Used to check the engine used
\usepackage{xpatch} % Used to patch highlight depending of the engine used
\usepackage{xcolor} % To use custom colors :P
\usepackage{lua-ul} % Package for underlining text in LuaLaTeX
\usepackage{luacolor} % Required by lua-ul since we use custom colors

% Define a color for the inline code background
\definecolor{bg}{HTML}{e5e7eb}

\setmintedinline{breaklines,breakanywhere=true,bgcolor=bg}

% Define the command CodeInline with optional language parameter
\newcommand{\CodeInline}[2][text]{%
    % Display code inline with backticks
    \texttt{\`{}}\mintinline{#1}{#2}\texttt{\`{}}%
}

% Define custom highlighting command using lua-ul (if LuaLaTeX engine is used)
\makeatletter
\newcommand\myhl[2]{\highLight[#1]{#2}}
% Patch minted's inline code display to use custom highlighting
\xpatchcmd{\minted@inputpyg@inline}{%
  \colorbox%
}{%
  \myhl%
}{\typeout{Success}}{\fail}
\makeatother

\begin{document}
Eum eligendi veritatis est omnis beatae velit est ipsam explicabo quis quia animi. Reiciendis dolor modi omnis ipsum adipisci quo ducimus. Dolores sequi mollitia est deserunt quia doloribus fugit ut pariatur voluptas ut perspiciatis iure. Maiores qui nulla est aut modi. Unde occaecati. incidunt dolor velit dolorum voluptas possimus excepturi voluptatum. Dolores qui numquam ipsa quia totam animi explicabo quisquam qui iste illum esse ipsam labore odit. Impedit quo nisi labore voluptatem \CodeInline{/path/to/file.ext:123:4:TYPE/path/to/file.ext:123:4:TYPE} aut deserunt dolorum quidem. Omnis dolorum sapiente et occaecati labore dicta fuga. Laborum reiciendis consequatur rerum ex molestias voluptates autem et. Magnam numquam minima illo magnam. \mintinline{text}{/path/to/file.ext:123:4:TYPE/path/to/file.ext:123:4:TYPE} Quisquam quos eos in non ullam aut aperiam. Voluptas quo quia dolores quis ipsa velit odio consequuntur laboriosam cum. Illo pariatur molestiae tempora voluptatibus autem cum pariatur aperiam ipsa ipsa ut labore. Ut temporibus voluptatibus quos architecto perspiciatis. Quia non porro quaerat maiores vero saepe facere aut occaecati consequatur est et voluptate quasi. Qui sunt dolor deleniti aut qui.
\end{document}

This produce the following result:

A working example of a large highlighted inline code using minted and lua-ul

muzimuzhi commented 4 months ago
% Define the command CodeInline with optional language parameter
\newcommand{\CodeInline}[2][text]{%
    % Display code inline with backticks
    \texttt{\`{}}\mintinline{#1}{#2}\texttt{\`{}}%
}

You may want to grab the inline code in verbatim, \NewDocumentCommand{\CodeInline}{ O{text} v }{ ... }. Try \CodeInline{\mycmd\par} to see the difference.

Heziode commented 4 months ago

Right!

Thank you for your help :)

The only thing I found annoying with this solution is that I cannot add a border and radius to the corners of the highlight. So the solution is a little bit ugly but it works.