4

I am using the function hl from package soul to highlight text. I would like to create a specific environment, that would be used for the highlighting. I tried with newenvironment, but did not figure how to get it right (see also this post). I tried also using the package environ to define a new environment, but am now facing an error message from soul: Package soul Error: Reconstruction failed. I came across hyphenatable material enclosed in group braces, which I can't handle. Either drop the braces or make the material unbreakable using an \mbox (\hbox).

Any idea how I can embed hl into an evnironment?

Thanks!

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\usepackage{environ} \usepackage{color,soul} \NewEnviron{newtext}{\hl{\BODY}} %\NewEnviron{newtext}{\BODY}

\begin{document}

\hl{I work well when called as a function }

\begin{newtext} I don't seem to work when called as an enviornment. \end{newtext}

\end{document}

Matifou
  • 722

3 Answers3

6

An expl3 version:

hl expl3

with environment option to change the colour.

MWE

\documentclass{article}
\usepackage{xparse} %part of kernel now
\usepackage{xcolor,soul}
\ExplSyntaxOn
\NewDocumentEnvironment { newtext } { o +b } {
    \IfNoValueF {#1}
     {      
        \sethlcolor{#1}
        }
    \hl { #2 } 
}{}

\ExplSyntaxOff

\begin{document}

\hl{I work well when called as a function }

\begin{newtext} I don't seem to work when called as an environment.

I don't seem to work when called as an environment.

I don't seem to work when called as an environment.

\end{newtext}


\begin{newtext}[green] I don't seem to work when called as an environment.

I don't seem to work when called as an environment.

I don't seem to work when called as an environment.

\end{newtext}

\end{document}

Cicada
  • 10,129
5

If you are willing to use LuaLaTeX you can use my lua-ul package to avoid having to use environ and friends alltogether and define a "regular" environment instead. Then there are also less restrictions about possible arguments:

\documentclass{article}
\usepackage{xcolor,luacolor,lua-ul}
\makeatletter
\newenvironment{newtext}[1][yellow] {%
   \luaul@setcolor{#1}%
   \@highLight
}{}
\makeatother

\begin{document}

\highLight{I work well when called as a function }

\begin{newtext} I work well when called as an environment.

I work well when called as an environment.

I work well when called as an environment.

\end{newtext}


\begin{newtext}[green] I work well when called as an environment.

I work well when called as an environment.

I work well when called as an environment.

Even allows using e.g.@ \verb|\verb| when used as an environment. \end{newtext}

\end{document}

The output of the code above demonstrating that the highlighting works

4

You need to expand \BODY before \hl absorbs it. The way to do this is with \expandafter, which skips over one token and (once-)expands the next, before executing the skipped-over token. Since \BODY is two tokens ahead of \hl, with an intervening left brace {, one must also include an \expandafter before the {, as well, so that the first \expandafter will expand the 2nd \expandafter which, in turn, will skip over the { and expand \BODY.

\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}

\usepackage{environ} \usepackage{color,soul} \NewEnviron{newtext}{\expandafter\hl\expandafter{\BODY}} %\NewEnviron{newtext}{\BODY}

\begin{document}

\hl{I work well when called as a function }

\begin{newtext} I don't seem to work when called as an enviornment. \end{newtext}

\end{document}

enter image description here