如何(整齐地/正确地)用 tikz 矩形加下划线?

如何(整齐地/正确地)用 tikz 矩形加下划线?

在下面的 MWE 中,我希望红色褪色 - tikz - 矩形线强调这三个“subtiles”。在我开始修修补补和猜测之前:如何巧妙地实现这一点?

在此处输入图片描述

\PassOptionsToPackage{svgnames}{xcolor}

\documentclass[a4paper]{article}

\usepackage[framemethod=TikZ]{mdframed}

\usepackage{colortbl}
 \newlength{\mycustomlength}


\settowidth\mycustomlength{abcdefghiblablab}


    
\usepackage{tabularx}

\usepackage{multirow}



\begin{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tabularx}{\textwidth}{@{}*{1}{p{\mycustomlength}}*{1}{X}@{}} % sub-headings modification

\multicolumn{2}{@{}l}{\sbox0{\textsc{\color{black}Some Subtitle}\phantom{M}}\usebox0 \tikz[baseline=-0.5ex] \shade[left color=red, right color=white] (0,0) rectangle ({\linewidth-\wd0},-0.02);}
\\
bla bla bla bla! & blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.
\\
\multicolumn{2}{@{}l}{\sbox0{\textsc{\color{black}Another Subtitle}\phantom{M}}\usebox0 \tikz[baseline=-0.5ex] \shade[left color=red, right color=white] (0,0) rectangle ({\linewidth-\wd0},-0.02);}
\\
bla bla bla bla! & blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.
\\
 \multicolumn{2}{@{}l}{\sbox0{\underline{\textsc{\color{black}And A Third Subtitle}}\phantom{M}}\usebox0 \tikz[baseline=-0.5ex] \shade[left color=red, right color=white] (0,0) rectangle ({\linewidth-\wd0},-0.02);}
\\
bla bla bla bla! & blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.
\\


\end{tabularx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\end{document}

期望的结果:

在此处输入图片描述


编辑

到目前为止的答案已经提供了用 tikz 取代标准下划线的良好尝试/解决方案。

此外,我很高兴看到 MWE,其中 tikz 下划线的长度与文本长度分离,也就是说,行的长度是独立于文本长度确定和固定的。

答案1

另一种解决方案。

我的命令和 Ignasi 的很像。我发誓我没有作弊!

输出

在此处输入图片描述

代码

\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage{tabularx}
\usepackage{multirow}
\begin{document}
\newcommand\miniTikzUl[2][]
{%
\begin{tikzpicture}[baseline=(a.base)]
  \node[inner sep=0pt] (a) {#2\strut};
  \begin{scope}[on background layer]
    \shade[left color=red!80, right color=white,#1] 
      ([yshift=-.3mm]a.base east) rectangle ([yshift=-.7mm]a.base west);
  \end{scope}
\end{tikzpicture}%
}
\paragraph{Test Underlining}~

\noindent\hrulefill

\underline{underlined} \underline{underlinedWithStrut\strut} 

\miniTikzUl[opacity=.5]{\underline{underlined} \underline{underlinedWithStrut\strut} duTexte}
blabla blabla blabla blabla blabla blabla blabla blabla

\miniTikzUl[left color=blue,]{duTexte blabla blabla}
blabla blabla blabla blabla blabla blabla blabla

\noindent\hrulefill

\paragraph{Test Tabularx}~\\ 
\newcommand\mySubtitle[2][]
{%
  %\multicolumn{2}{@{}l}{\miniTikzUl[#1]{#2}}%
  \strut\rlap{\miniTikzUl[#1]{#2}} & 
}

\newcommand\myRow%
{%
bla bla bla bla! & blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. %
bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. %
}

%\noindent 
%\begin{tabular}{ll} 
%  \mySubtitle{Some Subtitle}        \\
%  \myRow                            \\
%  \mySubtitle{Another Subtitle}     \\
%  \myRow                            \\
%  \mySubtitle{And a Third Subtitle} \\
%  \myRow                            \\
%\end{tabular}

\noindent
\begin{tabularx}{\textwidth}{@{}*{1}{p{3cm}}*{1}{X}@{}} % sub-headings modification
  \mySubtitle{Some Subtitle}                                      \\
  \myRow                                                          \\
  \mySubtitle{Another Subtitle gjpq}                                   \\
  \myRow                                                          \\
  \mySubtitle[opacity=.6, left color=black]{And a Third Subtitle} \\
  \myRow                                                          \\
\end{tabularx}

\end{document}

更多变体

输出

在此处输入图片描述

代码

\documentclass[12pt]{article}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usepackage{tabularx}
\usepackage{multirow}
\newlength\ambientLinewidth
\AtBeginDocument{\setlength\ambientLinewidth{\linewidth}}
\begin{document}
\thispagestyle{empty}
\newcommand\miniTikzUl[2][]
{%
\begin{tikzpicture}[baseline=(a.base)]
  \node[inner sep=0pt] (a) {#2\strut};
  \begin{scope}[on background layer]
    \shade[left color=red!80, right color=white,#1] 
      ([yshift=-.3mm]a.base east) rectangle ([yshift=-.7mm]a.base west);
  \end{scope}
\end{tikzpicture}%
}
\newcommand\mySubtitle[2][]
{%
  \strut\rlap{\miniTikzUl[#1]{\textsc{#2}}} & 
}

\newcommand\shortenedSubtitle[3][]
{
  \mySubtitle[#1]{#3\hspace{-#2}}
}
\newcommand\exactLength[3][]
{
  \mySubtitle[#1]{\hbox{}\rlap{#3}\hphantom{\rule{#2}{0pt}}}
}

\newcommand\myRow%
{%
texte à gauche & texte à droite
}

\noindent
\begin{tabularx}{\textwidth}{@{}*{1}{p{3cm}}*{1}{X}@{}} % sub-headings modification
  \exactLength{2mm}{Exactly 2mm}                                         \\
  \myRow                                                                 \\
  \shortenedSubtitle[right color=blue]{2cm}{Subtitle with 2cm less}      \\
  \myRow                                                                 \\
  \mySubtitle[right color=blue]{These commands seem a bit redundant, though\hspace{-5cm}\rlap{\raisebox{-.5mm}{\color{green}\rule{5cm}{.5pt}}}} \\
  I recommend \rlap{instead you read about : } & 
  \begin{itemize}
    \item \verb|\rlap| \item \verb|\hspace| \item \verb|\rule| \item \verb|\phantom|
  \end{itemize} 
  It will change your life (\emph{or maybe not !}) \\
  \\
  \strut\rlap{\textsc{The full line, \emph{no complicated command needed...}}}%
  \raisebox{-.7mm}
  {%
    \noindent\tikz\shade[left color=orange!50!yellow, right color=purple!50!blue] 
    (0,0) rectangle (\ambientLinewidth,-.4mm);
  } 
  \\ 
  \myRow \\ 
\end{tabularx}

\noindent\hrulefill

\paragraph{debugging}~

\verb|\the\linewidth|         \the\linewidth

\verb|\the\ambientLinewidth|  \the\ambientLinewidth

\begin{description}
  \item [baseline for tikzpics] a\tikz[baseline=(a.base)]\node[inner sep=0pt](a){a};
\end{description}

\end{document}

答案2

这个答案很大程度上受到了讨论的启发 这里

我首先从以下网址下载了 Martin Scharrer 的代码:https://bitbucket.org/martin_scharrer/tikzpagelayers

并定义以下命令,tikzshll类似于现有的tikzshl,只是具有您可能喜欢的阴影和厚度。

\newcommand\tikzshll[2][]{%
  \begingroup
  \tikzpagelayer[background]{\shade [line width=.5cm,left color=#1, right 
  color=white] (tplbegin) ++(0,-.1\dp\strutbox) 
    rectangle (tplend);}%
  #2%
  \endtikzpagelayer
  \endgroup
}

如果需要肯定下划线和文本之间的距离(注意;这需要\usetikzlibrary{calc},已在原始样式文件中使用。)

\newcommand\tikzshll[2][]{%
  \begingroup
  \tikzpagelayer[background]{\shade [line width=.5cm,left color=#1, right 
  color=white] ($(tplbegin) + (0, -0.1em)$) ++(0,-.1\dp\strutbox) 
    rectangle ($(tplend) + (0, -0.1em)$);}%
  #2%
  \endtikzpagelayer
  \endgroup
}

并像这样使用它;

\PassOptionsToPackage{svgnames}{xcolor}

\documentclass[a4paper]{article}

\usepackage[framemethod=TikZ]{mdframed}

\usepackage{colortbl}
 \newlength{\mycustomlength}
\settowidth\mycustomlength{abcdefghiblablab}

\usepackage{tabularx}

\usepackage{multirow}

% Added this
\usepackage{tikzpagelayers}

\begin{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tabularx}{\textwidth}{@{}*{1}{p{\mycustomlength}}*{1}{X}@{}} % 
%sub-headings modification

\multicolumn{2}{@{}l}{\tikzshll[red]{Some Subtitle}}
\\
bla bla bla bla! & blaaaaaaaaaaaaa \tikzshll[purple]{blaaaaaaaaaaaaaaa.} bla. 
blaaaaaaaaaaaaa 
blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa \tikzshll[black]{blaaaaaaaaaaaaaaa.} 
bla.blaaaaaaaaaaaaa 
blaaaaaaaaaaaaaaa. bla.
\\

\end{tabularx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\end{document}

(我不确定您是否需要整个样式文件tikzpagelayers来实现相同的效果。)请参阅tplbegintplend

其结果是;

mwe_结果

转变的结果-0.1em如下;

mwe_结果2

答案3

更新:

经过几版之后,我觉得 OP 想要一些可以用 来完成的东西sidebyside tcolorbox。OP 说 SUBTITLE 可以是tcolorbox标题,左右段落宽度可以自动调整到其内容,就像我的例子一样,也可以调整到固定宽度。淡入淡出的线条是作为overlay所需长度(在本例中为框的总宽度)的选项绘制的。

在此处输入图片描述

\documentclass[a4paper]{article}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}

\tcbset{
mysidebox/.style={
    sidebyside,
    sidebyside adapt=left, 
    sidebyside gap=5mm, 
    sidebyside align=top, 
%
    colback=white, 
    colbacktitle=white, 
    fonttitle=\scshape, 
    coltitle=black, 
    title=#1, 
%
    frame hidden,
    lower separated=false, 
    enhanced,
    overlay={\fill[left color=red, right color=white] (title.south west) rectangle ([yshift=-1pt]title.south east);},
%
    boxrule=0pt, 
    boxsep=0pt,
    left=0pt, 
    right=0pt, 
    bottom=0pt,
    toptitle=1pt,
    bottomtitle=1pt,
}
}

\begin{document}

\lipsum[1]
\tcbsidebyside[mysidebox=This is my title
]
{bla bla bla }{\lipsum[2]}
\tcbsidebyside[mysidebox=another subtitle
]
{bla bla bla bla bla bla }{\lipsum[2]}

\end{document}

上一个答案

像这样?

在此处输入图片描述

\PassOptionsToPackage{svgnames}{xcolor}
\documentclass[a4paper]{article}
\usepackage{tikz}
\newlength{\mycustomlength}
\settowidth\mycustomlength{abcdefghiblablab}

\usepackage{tabularx}

\newcommand{\subtitle}[1]{%
    \begin{tikzpicture} 
        \node[font=\scshape, text=black, inner xsep=0pt, outer sep=0pt] (aux) {#1};
        \shade[left color=red, right color=white] (aux.south west) rectangle ([yshift=-1pt]aux.south east);
    \end{tikzpicture}}

\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{tabularx}{\textwidth}{@{}*{1}{p{\mycustomlength}}*{1}{X}@{}} % sub-headings modification

\multicolumn{2}{@{}l}{\sbox0{\subtitle{Some Subtitle}\phantom{M}}\usebox0 \tikz[baseline=-0.5ex] \shade[left color=red, right color=white] (0,0) rectangle ({\linewidth-\wd0},-0.02);}
\\
bla bla bla bla! & blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.
\\
\multicolumn{2}{@{}l}{\sbox0{\subtitle{Another Subtitle}\phantom{M}}\usebox0 \tikz[baseline=-0.5ex] \shade[left color=red, right color=white] (0,0) rectangle ({\linewidth-\wd0},-0.02);}
\\
bla bla bla bla! & blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.
\\
 \multicolumn{2}{@{}l}{\sbox0{\subtitle{And A Third Subtitle}\phantom{M}}\usebox0 \tikz[baseline=-0.5ex] \shade[left color=red, right color=white] (0,0) rectangle ({\linewidth-\wd0},-0.02);}
\\
bla bla bla bla! & blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla. blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.blaaaaaaaaaaaaa blaaaaaaaaaaaaaaa. bla.
\\
\end{tabularx}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\end{document}

相关内容