[Tex/LaTex] Something like \raisebox that respects line breaks

line-breakingraise

If one wants to raise some text one uses something like

\raisebox{0.5 em}{some text}

However this doesn't respect line breaks and may cause some nasty overflows.

Is there a way to have \raisebox respect line breaks? Or an alternative that provides the same functionality?

Edit: Here's an example for clarification:

\documentclass{article}

\begin{document}

Lorem ipsum dolor sit amet,
\raisebox{0.5 em}{consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}

\end{document}

I need the raised text to line break like it would if it wasn't raised.

Edit2: Apparently my clarification wasn't to helpful. I want to raise an entire sentence, if I use \raisebox the sentence will overflow to the right (as seen in the example).

If this was HTML I would use <sup>some text</sup> which is able to handle line breaks.

Best Answer

As @egreg said this is not fully automatically possible with TeX for a simple reason: TeX offers no way to automatically determine the hyhenation points in a word and use them other than in the default manner.

But if you are prepared to mark up hyphenations yourself there are ways to get the rest automatically done. Below is a trivial implementation:

\documentclass{article}

\setlength\textwidth{8cm}

\def\X#1{\raisebox{0.5em}{#1}}

\def\Y#1#2#3{\discretionary{\X{#1}}{\X{#2}}{\X{#3}}} 

\begin{document}

Lorem ipsum dolor sit amet, consectetur 
\Y{adipisic-}{ing}{adipisicing} \X{elit,} \X{sed} do eiusmod tempor incididunt ut
 labore et dolore magna aliqua.

\end{document}

This results in

enter image description here

which is what I think you are looking for as the output. Of course this is not the way you would want to code it. With some small coding improvements you could use a syntax like

Lorem ipsum dolor sit amet, consectetur 
\begin{raisedtext}{0.5em}
  adipisic\-ing elit, sed
\end{raisedtext}
do eiusmod tempor incididunt ut labore et dolore magna aliqua.

where

  • possible hyphenation points are maked up by \-
  • a - would automatically be allowed as hyphenation point

Basically you would need to parse the text until the end of the environment looking for - or \- or "spaces" and construct stuff like \Y and \X from above accordingly (not doing that tonight :-)

Upate: a simple parser

okay so here is a simple parser that takes care of \- and explicit -. Not really cleanly written, sorry for that.

\documentclass{article}

\setlength\textwidth{8cm}

% we need space, -, and newline active and set to some commands
{\obeyspaces
\catcode`\^^M\active%
\catcode`\-\active%
\gdef\setraisedtextactivedef#1#2{\let =#1\let^^M=#1\let-=#2}}

\newbox\raisedtextbox

% main action is to collect material into a box
\def\collectraisedtext{\setbox\raisedtextbox\hbox\bgroup\raisedtextstyle
\gobbleactivespaces}

% and if we want we can use a special style
\def\raisedtextstyle{\small\itshape}

% at a space end collection, typeset and restart
\def\raisedtextspace{\egroup
       \X{\box\raisedtextbox}%
      \space
      \collectraisedtext
}                              

% at \- end colloection, typeset, add discretionary and restart
\def\raisedtextbreak{\egroup\X{\box\raisedtextbox}\discretionary{\X-}{}{}\collectraisedtext}
% at - (explicit hyphen) more or less the same
\def\raisedtexthyphen{\egroup\X{\box\raisedtextbox}\discretionary{\X-}{}{\X-}\collectraisedtext}

% several active spaces (or newlines) would do harm ...
\def\gobbleactivespaces{\futurelet\next\gobbleactivespacesX}
\def\gobbleactivespacesX{%
            \ifx\next\raisedtextspace
               \expandafter\gobbleactivespacesXX
           \fi
}
\def\gobbleactivespacesXX#1{\gobbleactivespaces}

% putting all together
\newenvironment{raisedtext}[1][0.5ex]
  {%
    \def\X##1{\raisebox{#1}{##1}}%
    \obeyspaces
    \catcode`\^^M\active
    \catcode`\-\active
    \setraisedtextactivedef\raisedtextspace\raisedtexthyphen
    \let\-\raisedtextbreak
    \collectraisedtext
}{%
% at end environment, end collection and typeset (if not empty). 
% Otherwise remove space already inserted before that collection
       \egroup
       \ifdim\wd\raisedtextbox>0pt  % weak prove that this is not empty
         \X{\box\raisedtextbox}%
       \else
         \unskip
       \fi
}

\begin{document}

Lorem ipsum dolor sit amet, consectetur 
\begin{raisedtext}
      adipisic-ing       elit,      sed
\end{raisedtext}
 do eiusmod tempor incididunt ut
 \begin{raisedtext}[1.5ex] la-bore-et do-lore
\end{raisedtext}  magna aliqua.

\end{document}

doing that gives us (with a style definition of \small\itshape)

enter image description here

Related Question