带有圆角的彩色背景,用于防止文本片段溢出行尾

带有圆角的彩色背景,用于防止文本片段溢出行尾

我已经找到了一些类似的解决方案,但无法使它们适应我的要求:

  • 被其他文本包围的文本片段(例如源代码)应使用背景色突出显示。背景应有略圆的角。背景有边框是不错的选择,但不是必需的。
  • 我们可以假设文本片段的长度不会超过一整行。但是,文本片段可能不适合当前行的剩余宽度。我​​希望找到以下解决方案之一来解决这个问题:
    1. 与往常一样,文本片段不合适的部分将换到下一行(期望的输出示例:如下方第一张图所示,但带有圆角)。
    2. LaTeX 确定文本片段不适合当前行,并自动将整个文本片段放在下一行。看起来不太好看,因为它不再完全对齐,但还可以。(期望的输出示例:如下面的第二幅图所示,但带有圆角)。

相关解决方案:

使用第一个链接中的建议(通过灵魂或通过复杂解决方案)我可以得到这个输出。它几乎完美,但没有圆角。这个问题告诉我使用灵魂可能无法实现。有没有什么方法可以修改其他解决方案? 在此处输入图片描述

与第三个链接中的解决方案相结合soul可行,但也没有圆角(\lword在第三个链接中定义):

\newcommand{\inlinecode}[1]{{\lword{\sethlcolor{veryLightGray}\hl{\small\ttfamily#1}}}}

在此处输入图片描述

答案1

这是一个答案,通过结合使用椭圆突出显示和换行符圆角代码灵魂突破

编辑:添加了不在换行符周围四舍五入突出显示的逻辑。

\documentclass{article}
\parindent0pt
\usepackage{fullpage}
\usepackage{tikz}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.misc}
\usetikzlibrary{fit}

\makeatletter
\newbox\mybox
\newcount\test
\test0
\newcommand{\defhighlighter}[3][]{%
    \tikzset{every highlighter/.style={color=#2, fill opacity=#3, #1}}%
}

\defhighlighter[font=\ttfamily]{blue!20}{.3}

\newcommand{\highlight@DoHighlight}[1]{
    \node[outer sep = -13pt, inner sep = 0pt,
        fit=(begin highlight) (end highlight),
        every highlighter, this highlighter,
        rounded rectangle,#1,
        fill
    ]{} ;
}

\newcommand{\highlight@BeginHighlight}[1]{
  \coordinate[yshift=-\dp\mybox-1pt,#1] (begin highlight) at (0,0) ;
}

\newcommand{\highlight@EndHighlight}[1]{
  \coordinate[yshift=\ht\mybox+1pt,#1 ] (end highlight) at (0,0) ;
}

\newdimen\highlight@previous
\newdimen\highlight@current

\DeclareRobustCommand*\highlight[2][]{%
  \setbox\mybox\hbox{#2}
  \tikzset{this highlighter/.style={#1}}%
  \SOUL@setup
  %
  \gdef\ht@possiblenoleftround{}
  \def\SOUL@preamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@BeginHighlight{xshift=3pt}
      \highlight@EndHighlight{xshift=-3pt}
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@postamble{%
    \begin{tikzpicture}[overlay, remember picture]
      \highlight@EndHighlight{xshift=-3pt}
      \expandafter\highlight@DoHighlight\expandafter{\ht@possiblenoleftround}
    \end{tikzpicture}%
  }%
  %
  \def\SOUL@everyhyphen{%
    \discretionary{%
      \SOUL@setkern\SOUL@hyphkern
      \SOUL@sethyphenchar
      \tikz[overlay, remember picture] \highlight@EndHighlight{} ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everyexhyphen##1{%
    \SOUL@setkern\SOUL@hyphkern
    \hbox{##1}%
    \discretionary{%
      \tikz[overlay, remember picture] \highlight@EndHighlight{} ;%
    }{%
    }{%
      \SOUL@setkern\SOUL@charkern
    }%
  }%
  %
  \def\SOUL@everysyllable{%
    \begin{tikzpicture}[overlay, remember picture]
      \path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
        \global\highlight@previous=\y0
        \global\highlight@current =\y1
      \endpgfextra (0,0) ;
      \ifdim\highlight@current < \highlight@previous
        \expandafter\highlight@DoHighlight\expandafter{\ht@possiblenoleftround,rounded rectangle right arc=none}
        \gdef\ht@possiblenoleftround{rounded rectangle left arc=none}
        \highlight@BeginHighlight{}
      \fi
    \end{tikzpicture}%
    \ttfamily \the\SOUL@syllable
    \tikz[overlay, remember picture] \highlight@EndHighlight{} ;%
  }%
  \SOUL@{#2}
}

\begin{document}
as suggested in a comment, justifies the text within the environment. this would work if there are more ordinary words than long ones, and the long ones don't occur in inconvenient locations within the lines. a test with the affected material, \highlight{trying both techniques side by side, will quickly make it clear which is preferable in a given situation.}s


this is some text \highlight{var files = Directory.EnumerateFiles(myDirectory,''*.txt*);} text
this is some text box text this is some text this is some text texttext \highlight{var files = Directory.EnumerateFiles(myDirectory, ''*.txt*);} hello
\end{document} 

答案2

以下是大卫·卡莱尔的换行密码圆角宏

\documentclass{article}
\parindent0pt
\usepackage{fullpage}
\usepackage{tikz}

\newenvironment{foo}
{\par
\hyphenpenalty=10000
\exhyphenpenalty=10000
}
{\par}

\newcommand\lword[1]{\leavevmode\nobreak\hskip0pt plus\linewidth\penalty50\hskip0pt plus-\linewidth\nobreak #1}
\newcommand\inlinecode[2][]{\lword{\tikz[overlay]\node[fill=blue!20,inner sep=1pt, anchor=text, rectangle, rounded corners=1mm,#1] {\ttfamily #2};\phantom{\ttfamily #2}}}
\begin{document}
\begin{foo}
this is some text \inlinecode{var files = Directory.EnumerateFiles(myDirectory,''*.txt*);} 
text as suggested in a comment, justifies the text within the environment. this would work 
if there are more ordinary words than long ones, and the long ones don't occur in 
inconvenient locations within the lines. a test with the affected material, 
\inlinecode{trying both techniques side by side, will quickly} make it clear which is 
preferable in a given situation.s
this is some text box text this is some text this is some text texttext 
\inlinecode{var files = Directory.EnumerateFiles(myDirectory, ''*.txt*);} hello
\end{foo}
\end{document} 

相关内容