2

How can I something like this (an \fbox that can span multiple lines):

                    |---------------------------------------------
The quick brown fox |jumps over the| lazy dog. The quick brown fox
                    |---------------------------------------------
--------------|
jumps over the| lazy dog. The quick brown fox jumps over the lazy dog.
--------------|
The quick brown fox jumps over the lazy dog.

MWE:

\documentclass{article}

\begin{document}

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. \fbox{Sed a leo ut erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla hendrerit.} Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim. Nam vitae bibendum erat, sit amet scelerisque felis. Suspendisse placerat vitae velit pharetra lobortis. Donec ut erat a erat porta aliquet. Nulla eget augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam. Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti. In posuere dapibus urna, vitae euismod magna iaculis at. Pellentesque pretium, lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel dolor.

\end{document}
  • You could do it with the fantastic answer here Cool Text Highlighting in LaTeX. Just remove random steps, in line 16, green, in line 97 and change \defhighlighter{yellow}{.5} from line 13 to \defhighlighter{yellow}{0}. – Bobyandbob Jan 20 '18 at 23:31
  • @Bobyandbob Why comment and not answer? Anyway, I don't have soul package – qnferthm Jan 20 '18 at 23:55
  • But you could install it ;) Alternative Box highlighting of text with rounded corners (soul-based) with \setlength\CornerRadius{0pt} in line 11 should also do the job. – Bobyandbob Jan 20 '18 at 23:59
  • @Bobyandbob I can install it at home, but I don't think they have it at my work location – qnferthm Jan 21 '18 at 0:19
  • 1
    To get an answer, it helps to provide a Minimum Working Example. Then all we have to do is copy it and fix it. – John Kormylo Jan 21 '18 at 1:29
6

This is by no means a competitor to the really cool post pointed out by Bobyandbob. Rather it is an attempt to convince myself that one can achieve this with TikZ "only".

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{tikzpagenodes}
\makeatletter
\newcommand{\gettikzxy}[3]{% from https://tex.stackexchange.com/a/58590/121799
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \global\edef#2{\the\pgf@x}%
  \global\edef#3{\the\pgf@y}%
}
\makeatother
\newcommand{\BoxMe}[2][2pt]{%
\tikz[remember picture,overlay, baseline=(Begin.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (Begin) {\strut};}#2%
\tikz[remember picture,overlay, baseline=(End.base)]{%
\node[anchor=base,inner sep=0pt,outer sep=0pt] (End) {\strut};}%
\begin{tikzpicture}[overlay,remember picture]
\gettikzxy{($(Begin.north)-(current page.south west)$)}{\BeginxN}{\BeginyN}
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}%\typeout{\BeginyN\space\EndyN}
\pgfmathtruncatemacro{\mytest}{\EndyN-\BeginyN}
\ifnum\mytest=0\relax% begin and and in the same line %\typeout{begin and end in the same line}
\draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$)
-- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- cycle;
\else% \typeout{end below begin}
\path (current page text area.north west) -- (current page text area.south west)
node(WestLine)[left]{};
\path (current page text area.north east) -- (current page text area.south east)
node(EastLine)[right]{};
\gettikzxy{($(End.north)-(current page.south west)$)}{\EndxN}{\EndyN}
\gettikzxy{($(Begin.south)-(current page.south west)$)}{\BeginxS}{\BeginyS}
\pgfmathtruncatemacro{\mytest}{\BeginyS-\EndyN+1pt}% \typeout{\mytest}
\ifnum\mytest<2\relax% \typeout{end in the next line after begin}%
\pgfmathtruncatemacro{\mytest}{\BeginxS-\EndxN}% \typeout{\mytest}
\ifnum\mytest>0\relax%
\draw[thick,-] (Begin.north -| EastLine) -- ($(Begin.north)-(#1,0)$) -- 
($(Begin.south)-(#1,0)$) -- (Begin.south -| EastLine);
\draw[thick,-] (End.south -| WestLine) -- ($(End.south)+(#1,0)$) -- 
($(End.north)+(#1,0)$) -- (End.north -| WestLine);
\else
\draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$) -- 
(Begin.south -| WestLine) -- (End.south -| WestLine)
-- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- (End.north -| EastLine)
-- (Begin.north -| EastLine) -- cycle;
\fi
\else
\draw[thick,-] ($(Begin.north)-(#1,0)$) -- ($(Begin.south)-(#1,0)$) -- 
(Begin.south -| WestLine) -- (End.south -| WestLine)
-- ($(End.south)+(#1,0)$) -- ($(End.north)+(#1,0)$) -- (End.north -| EastLine)
-- (Begin.north -| EastLine) -- cycle;
\fi
\fi
\end{tikzpicture}}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. \BoxMe{Sed a leo ut
erat luctus scelerisque. Suspendisse auctor mauris sit amet fringilla
hendrerit.} Maecenas eu tortor eu diam fringilla scelerisque sit amet eget enim.
Nam vitae bibendum erat, sit amet scelerisque felis. \BoxMe{Suspendisse placerat
vitae velit pharetra lobortis et marmottae.} Donec ut erat a erat porta aliquet. Nulla eget
augue sem. Proin orci neque, eleifend id eleifend id, venenatis sit amet diam.
\BoxMe{Mauris lacus est, cursus eget sapien at, pharetra lobortis ante. Proin
tempus ipsum tellus, ut ultricies magna congue nec. Vestibulum ante ipsum primis
in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse potenti.
In posuere dapibus urna, vitae euismod magna iaculis at.} Pellentesque pretium,
lorem ut vehicula egestas, velit dolor porta nunc, id interdum ipsum purus vel
dolor.
\end{document}

enter image description here

It does the boxes as you suggest provided the box starts in one line, closes in the next and there is no horizontal overlap, see the second box in the picture above. If there is horizontal overlap, or if the box goes over more than two lines, it closes in a way that I personally like. Criticisms or suggestions for improvements are highly welcome, I'll be happy to revise this snippet! (At this point, I noticed two shortcomings. First, this obviously does not work if the box is supposed to go over a page break. Second, if the box goes over more than one line and starts at the very left of a line and/or ends at the very right of a line, the result is not pretty. Both things can be fixed, but I wait until I've got some more comments.)

  • You should not use \ifnum\mytest=0%. This way if the next line starts with something that TeX interprets as a number can lead to unexpected behaviour. Either use \ifnum\mytest=0 % or \ifnum\mytest=0\relax% (the latter egreg doesn't consider to be good practise...). – Skillmon likes topanswers.xyz Jan 24 '18 at 8:44
  • @Skillmon Thank you so much, that's a really great suggestion! I edited the code accordingly. BTW, I have posted a first proposal for a TikZ strikeout command. – user121799 Jan 26 '18 at 4:54
  • Also I think these should be \ifdim tests, and you should test against \baselineskip to detect how many lines are used. – Skillmon likes topanswers.xyz Jan 26 '18 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.