0

In the following MWE, I'd like the red-fading-tikz-rectangle-line to underline the three "subtiles". Before I start tinkering and guessworking around: How to accomplish this neatly?

enter image description here

\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}


Desired outcome:

enter image description here


Edit

The answers so far have provided good attempts / solutions to replace the standard underlining with tikz.

Additionally, I would be happy to see an MWE where the length of the tikz-underline is decoupled from the text length, that is, where the length of the line is determined and fixed independently of the length of the text.

3 Answers3

4

Another solution.

My command is very much like Ignasi's. I swear I didn't cheat !

The output

enter image description here

The code

\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}

Some more variants

The output

enter image description here

The code

\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}
marsupilam
  • 6,383
2

Update:

After several editions, it seems to me that OP wants something which can be done with a sidebyside tcolorbox. What OP says SUBTITLE could be the tcolorbox title and left and right paragraphs widths can be automatically adjusted to their contents like on my example or to a fixed width. The faded line is drawn as an overlay option with the desired length (total box width in this case).

enter image description here

\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}

Previous answer

Like this?

enter image description here

\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}
Ignasi
  • 136,588
  • Could you tweak it so that it would have the same vertical spacings as the original black-underline of the third subtitle ? – nutty about natty Jun 28 '17 at 11:11
  • ... as in, "superimposed" on the original conventional black underline ... (leaving everything else, like line spacing, unchanged) – nutty about natty Jun 28 '17 at 11:17
  • @nuttyaboutnatty Do you know the distance? and line width? – Ignasi Jun 28 '17 at 11:18
  • No................... – nutty about natty Jun 28 '17 at 11:19
  • 1
    @nuttyaboutnatty As soon as you know them, use the distance to fix the inner ysep in node aux and replace -1pt by line width in yshift option. – Ignasi Jun 28 '17 at 11:24
  • @nuttyaboutnatty another solution, make some trial and error test until you are satisfied with results. – Ignasi Jun 28 '17 at 11:25
  • @nuttyaboutnatty Please look at my updated answer, i think it fits better your desired output. – Ignasi Jun 28 '17 at 14:56
  • @nuttyaboutnatty I think the advice about the tcolorbox option, with the great settings, should be the definitive answer to this thread ! Well done ! – marsupilam Jun 28 '17 at 15:04
  • looks neat, tcolorbox seems powerful (though I probably need to bring my tex-installation up to date so all the features of this MWE are supported: currently not), HOWEVER, (apart from the "subtitles") still require a neat table layout with two columns (as in your first MWE). – nutty about natty Jun 28 '17 at 16:05
2

This answer heavily inspired from the discussions here

I started by downloading Martin Scharrer's code from https://bitbucket.org/martin_scharrer/tikzpagelayers.

And defined following command called tikzshll similar to the existing tikzshl just with shade and thickness you might like.

\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
}

If one needs certain distance between the underline and text (note; that requires \usetikzlibrary{calc}, already used in the original style file.)

\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
}

And using it like so;

\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}

(I am not sure if you need whole style file tikzpagelayers to achieve the same.) Please see tplbegin and tplend.

It results;

mwe_results

And the results with the shift -0.1em looks like;

mwe_results2

trblnc
  • 451
  • Could you tweak it so that it would have the same vertical spacings as the original black-underline of the third subtitle ?

    ... as in, "superimposed" on the original conventional black underline ... (leaving everything else, like line spacing, unchanged)

    – nutty about natty Jun 28 '17 at 11:31
  • Hard to tell :), especially after seeing this. Are you insist on \underline or is it okay to use \uline for you? If you are looking for the effect of uline, I think it can be done by tuning (tplbegin) to (tplbegin) + (0, shift), let's say shift is equal to -1em. – trblnc Jun 28 '17 at 11:47
  • the thing should look "neat" and the text lines shouldn't be shifted by the underlining. that is to say, the exact vertical position is not really important and this shouldn't really pose a problem here. – nutty about natty Jun 28 '17 at 13:22