3

The Question : How to make a glowing text?

@'Symbol 1', interesting, I encapsulated your script into a new command (named '\glow') so I could highlight text just like with '\hl{}'. However the result of '\glow{This is a \hl{test}}' was not satisfactory due to the fact that the yellow box was rewritten multiple time above the glowing effect, hiding it.

So I tried to modify your solution, print the original string first (say with '\hl{}'), use \pdfstringdef to remove the formatting and get a clean/plain string for the glowing string, then display it one last time above to get a clean text.

Yet \pdfstringdef turns accented characters into octal sequences as seen here :

How to strip a string of all formatting

Here's a little tweaked version with transparency to "respect" highlight or other background effects. Yet the problem remains for accented characters or even parenthesis and alike :

glow_preserving_highlight

\documentclass{article}

\usepackage{tikz}               % Graphique
\usepackage{transparent}        % Transparence
%\usepackage[outline]{contour}  % Contour

\usepackage{xcolor}             % Couleurs par nom

\usepackage{soul}               % \hl{}
\usepackage{hyperref}           % \pdfstringdef

% =============================================================================
\makeatletter   % Autorise la modification des macros système
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

% /!\ UTILISATION DE 'PDFSTRINGDEF' POUR 'NETTOYER' LE TEXTE
\newcommand{\glow}[2]{
        % Début liste de 'nettoyage' (rajouter les commandes à 'nettoyer')
        \pdfstringdefDisableCommands{\def\hl{}} % Nettoyage de '\hl{#1}'
        % Fin liste de 'nettoyage'
        \pdfstringdef\plainstr{#2}  % Extraction chaine 'nettoyée'
%       #2%i                        % Texte avec mise en forme (fond) NOK
        \leavevmode                 % Mode horizontal (pas de passage à la ligne implicite)
        \pgfsys@beginscope          % = pdfliteral{q}
            \rlap{#2}%i             % Texte avec mise en forme (fond) 1x
            \pgfsetroundjoin        % = pdfliteral{1 j}
            \pgfsetroundcap         % = pdfliteral{1 J}
            \pdfliteral{1 Tr}%i     % no pgf alternative
            \foreach\ind in {10, ..., 1}{%i
                    \pgfmathsetmacro\per{(11-\ind)*5}%i
\iffalse
                        % Couleur décroissante
                        \color{#1!\per}%
\else
                        % Transparence cumulée
                        \color{#1}%
                        \transparent{0.1}%              % 10% max
\fi
\iftrue
                        % Contour 'léger'
                        \pgfsetlinewidth{\ind/2}        % light
\else
                        % Contour 'épais'
                        \pgfsetlinewidth{(\ind/2)+1}    % heavy
\fi
%                   \rlap{#2}%i                         % x fois COULEUR avec mise en forme NOK
                    \rlap{\plainstr}%i                  % x fois COULEUR sans mise en forme
                }%i
        \pgfsys@endscope            % = pdfliteral{Q}
        \plainstr                   % Texte sans mise en forme (dessus) NOIR
%       #2%i                        % Texte avec mise en forme (dessus) NOIR
    }

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\makeatother    % Bloque la modification des macros système
% =============================================================================

\begin{document}

COMPILE AT LEAST TWICE !!!

This is a test.

This is another \hl{test}.

\glow{green}{This one is supposed to glow without highlight.}

\glow{cyan}{This one is also supposed to \hl{glow} preserving the highlight.}

pdfstringdefDisableCommands and pdfstringdef removes the formatting.

Then I overwrite the glowing string with a clean black string without hl{}.

\glow{orange}{But accented characters are a hell to deal with.}

\glow{red}{Bùt àccéntèd charaçters are a hell to deal wïth.}

Even parenthesis gets "destroyed" in the process...

\end{document}

Btw I'm trying to mimic Word's text glowing function :

glow_highlight_using_word

Any solution ?

PS : I cannot add the tags 'latex', or 'glow' or 'pdfstringdef' unless I have 300 "reputation"

EDIT : with the solution provided, which work in general (accented characters are rendered correctly) I get a "feedback" effect now. I should investigate. "Not that elementary, my dear Watson".

glow_preserving_highlight_feedback

5
  • 2
    well yes that is the purpose of \pdfstringdef: it encode the string in a way needed inside a pdf. But please make your question self contained with a proper minimal example. I don't want to wander through various links trying to figure out what you are trying to do. Commented Oct 18, 2020 at 9:52
  • Edited, hope it helps to understand the issue. And no, I don't need octal representation when I use an UTF-8 file with \usepackage[utf8]{inputenc}, especially from something called \pdfstringdef or I would have used something like \getoctalsequence which is more explicit in its intentions.
    – Kochise
    Commented Oct 18, 2020 at 14:14
  • If you just want to expand some text to a string try \text_expand:n Commented Oct 18, 2020 at 14:18
  • 1
    \pdfstringdef is the wrong command. It is meant to produce a pdf string. Drop the idea to use it for your goal. Commented Oct 18, 2020 at 14:20
  • Probably, yet I still need something to strip all formatting to get a raw/plain string. Many people have the same need yet no clear answer to that.\\ I'll try with 'Phelype Oleinik' hint... (gosh, the hell to line feed in a comment)
    – Kochise
    Commented Oct 18, 2020 at 14:31

2 Answers 2

3

Try with \text_purify:n (requires a rather current text system!)

\documentclass{article}

\usepackage{tikz}               % Graphique
\usepackage{transparent}        % Transparence
%\usepackage[outline]{contour}  % Contour

\usepackage{xcolor}             % Couleurs par nom

\usepackage{soul}               % \hl{}
\usepackage{hyperref}           % \pdfstringdef

% =============================================================================
\makeatletter   % Autorise la modification des macros système
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

\ExplSyntaxOn
\cs_set_eq:NN\textpurify\text_purify:n
\ExplSyntaxOff
% /!\ UTILISATION DE 'PDFSTRINGDEF' POUR 'NETTOYER' LE TEXTE
\newcommand{\glow}[2]{
        % Début liste de 'nettoyage' (rajouter les commandes à 'nettoyer')
        %\pdfstringdefDisableCommands{\def\hl{}} % Nettoyage de '\hl{#1}'
        % Fin liste de 'nettoyage'
        \edef\plainstr{\textpurify{#2}}  % Extraction chaine 'nettoyée'
%       #2%i                        % Texte avec mise en forme (fond) NOK
        \leavevmode                 % Mode horizontal (pas de passage à la ligne implicite)
        \pgfsys@beginscope          % = pdfliteral{q}
            \rlap{#2}%i             % Texte avec mise en forme (fond) 1x
            \pgfsetroundjoin        % = pdfliteral{1 j}
            \pgfsetroundcap         % = pdfliteral{1 J}
            \pdfliteral{1 Tr}%i     % no pgf alternative
            \foreach\ind in {10, ..., 1}{%i
                    \pgfmathsetmacro\per{(11-\ind)*5}%i
\iffalse
                        % Couleur décroissante
                        \color{#1!\per}%
\else
                        % Transparence cumulée
                        \color{#1}%
                        \transparent{0.1}%              % 10% max
\fi
\iftrue
                        % Contour 'léger'
                        \pgfsetlinewidth{\ind/2}        % light
\else
                        % Contour 'épais'
                        \pgfsetlinewidth{(\ind/2)+1}    % heavy
\fi
%                   \rlap{#2}%i                         % x fois COULEUR avec mise en forme NOK
                    \rlap{\plainstr}%i                  % x fois COULEUR sans mise en forme
                }%i
        \pgfsys@endscope            % = pdfliteral{Q}
        \plainstr                   % Texte sans mise en forme (dessus) NOIR
%       #2%i                        % Texte avec mise en forme (dessus) NOIR
    }

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\makeatother    % Bloque la modification des macros système
% =============================================================================

\begin{document}

COMPILE AT LEAST TWICE !!!

This is a test.

This is another \hl{test}.

\glow{green}{This one is supposed to glow without highlight.}

\glow{cyan}{This one is also supposed to \hl{glow} preserving the highlight.}

pdfstringdefDisableCommands and pdfstringdef removes the formatting.

Then I overwrite the glowing string with a clean black string without hl{}.

\glow{orange}{But accented characters are a hell to deal with.}

\glow{red}{Bùt àccéntèd charaçters are a hell to deal wïth.}

Even parenthesis gets "destroyed" in the process...

\end{document}

enter image description here

9
  • Seems like that's the solution, yet a tricky one though :) I tried the '\text_expand:n' path but couldn't anything to work under pdflatex. How many latex distros are out there by the way ? Seems like more than Linux distros...
    – Kochise
    Commented Oct 18, 2020 at 15:08
  • Wow, can't even upvote your answer until I get "15 reputation". What kind of unwelcoming place is that ? Seriously ?
    – Kochise
    Commented Oct 18, 2020 at 15:09
  • I have windows, but this shouldn't matter, as long as it is current (which on linux could mean that you need to install a vanilla texlive). And it is not more tricky than your pdfstringdef version, both commands are quite similar (and \pdfstringdef is internally a very tricky and long command). And don't worry to much about the voting system, it is not really important if there is an upvote or not. Commented Oct 18, 2020 at 15:15
  • Ok, thanks for the explanations, I'm still rather new to the LaTeX inner working, it needs a good refresh/update because it's "tricky" to say the least. Heard something about LaTeX3 is in the pipes though...
    – Kochise
    Commented Oct 18, 2020 at 15:17
  • Btw the Word's rendering of the similar functionality is still superior IMHO, will have to understand how to create layers with alpha channel to get something more consistent without all these color bleeding due to transparent superposition :/
    – Kochise
    Commented Oct 18, 2020 at 15:20
0

Just a quick note about 'xspace', despite I know it's a "necessary evil" in many cases, just like in my own. The project name is embedded in a macro so that the changing the project name is straightforward (replace the macro content and voilà). But being a macro, the spaces following gets nuked into oblivion. While not a problem before a punctuation, it gets confusing inside a sentence when two adjacent words gets glued together without reason (even though we can always find a valid reason somehow, like planetary alignment and so on).

Anyway, I created a little MWE that demonstrate it really was the stripping of the 'xspace' that caused the "strange feedback effect". And now, another giant leap forward for mankind...

(Btw, I also tweaked the color/transparency settings and I think I nailed it, looks a lot more like Word's glowing, and no more color bleeding : "Yeah Baby, Yeah!")

glow_purify__mwe.png

\documentclass{article}

\usepackage{tikz}               % Graphique
\usepackage{transparent}        % Transparence

\usepackage{xcolor}             % Couleurs par nom
\usepackage{soul}               % \hl{}

\usepackage{xspace}             % Gestion des espaces

\def\test{\hl{test}\xspace}     % <<--- THE MACRO !!!

% =============================================================================
\makeatletter   % Autorise la modification des macros système
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

\ExplSyntaxOn
\cs_set_eq:NN\textpurify\text_purify:n
\ExplSyntaxOff
% /!\ UTILISATION DE 'TEXT_PURIFY' POUR 'NETTOYER' LE TEXTE
\newcommand{\glow}[2]{
        \edef\plainstr{\textpurify{#2}}  % Extraction chaine 'nettoyée'
%       #2%i                        % Texte AVEC mise en forme (fond) (NOK)
        \leavevmode                 % Mode horizontal (pas de passage à la ligne implicite)
        \pgfsys@beginscope          % = pdfliteral{q}
            \rlap{#2}%i             % Texte AVEC mise en forme (fond) 1x
            \pgfsetroundjoin        % = pdfliteral{1 j}
            \pgfsetroundcap         % = pdfliteral{1 J}
            \pdfliteral{1 Tr}%i     % no pgf alternative
            \foreach\ind in {10, ..., 1}{%i
                    \pgfmathsetmacro\per{(11-\ind)*5}%i
                    \color{#1!\per}%                % Couleur décroissante
                    \transparent{0.2}%              % Transparence cumulée
\iftrue
                    \pgfsetlinewidth{\ind/2}        % Contour 'léger'
\else
                    \pgfsetlinewidth{(\ind/2)+1}    % Contour 'épais'
\fi
%                   \rlap{#2}%i                     % x fois COULEUR AVEC mise en forme (NOK)
                    \rlap{\plainstr}%i              % x fois COULEUR sans mise en forme
                }%i
        \pgfsys@endscope            % = pdfliteral{Q}
%       #2%i                        % Texte AVEC mise en forme (dessus) NOIR
        \plainstr                   % Texte sans mise en forme (dessus) NOIR
    }

% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
\makeatother    % Bloque la modification des macros système
% =============================================================================

\begin{document}

COMPILE AT LEAST TWICE !!!

This is a \hl{test}.

This is another \test with a macro instead.

\glow{green}{This test is sùpposed to glow without highlight.}

\glow{cyan}{This \hl{test} is sûpposed to glow preserving the highlight.}

\glow{cyan}{This \test macro featüres a xspace that is context dependant.}

However text\_purify seems to be overzealous and "purify" it.

\end{document}
6
  • \xspace is not 'expandable' so it has to be removed to give a pure string. The general issue with xspace is that it's never 100% reliable: here, you could just have an explicit space after your test macro.
    – Joseph Wright
    Commented Oct 22, 2020 at 12:13
  • Explicit, like '\,' ? To replace '\xspace' ? Or write "\test\,macro" instead ? That's pretty unfriendly, it would be nice to have something that allows a more fluent integration of macros inside the text, which LaTeX is made for to begin with, without having to resort to all these kind of tricks. And don't even get me started about...
    – Kochise
    Commented Oct 22, 2020 at 14:23
  • Space skipping after macros is a design decision Knuth made for TeX: LaTeX can't change that. So overall it's easier to learn that one needs \foo\ to force a space than to try to get 'magic' to happen with xparse.
    – Joseph Wright
    Commented Oct 22, 2020 at 16:07
  • Thanks for the insight. Does LaTeX3 (will) solves this issue ? Because I do really like the 'magic' xspace allows... Already having to end lines with % everywhere tend to be quite messy, especially because there is no obvious location where these are required or not (see above MWE, the necessary %i where found by trial and error).
    – Kochise
    Commented Oct 22, 2020 at 16:27
  • I never use xspace, as Joseph (and the author of xspace tex.stackexchange.com/questions/86565/drawbacks-of-xspace) I find it much easier to get right spaces without it. But I also seldom define and use "space swallowing" commands, but almost always commands with at least one argument, where you don't need to care about spaces. Commented Oct 22, 2020 at 17:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .