47

Is there a horizontal equivalent to \raisebox? I'd like to shift some content horizontally without altering the layout of surrounding content.

Edit:

I feel bad because my question wasn't precise enough. David Carlisle's answer is the best if you're looking for the true horizontal analogue to \raisebox (moves content horizontally instead of vertically, operates in vertical mode instead of LR mode). However, what I really want is something that works in LR mode.

3
  • 2
    @RichardHansen: You could do \hspace*{<length>}\mbox{<content>}\hspace{-<length>} Apr 7, 2012 at 20:28
  • 1
    BTW, LaTeX is the default here, so you don't have to mentioned it. For other things like plainTeX there is a special tag. Apr 7, 2012 at 20:30
  • @MartinScharrer: Feel free to add that as an answer. :) How does it compare to Werner's answer? Apr 7, 2012 at 21:08

4 Answers 4

28

Here is a macro \shifttext{<len>}{<stuff>} that shifts <stuff> by <len> (negative for left, positive for right):

enter image description here

\documentclass{article}
\makeatletter
\newcommand*{\shifttext}[2]{%
  \settowidth{\@tempdima}{#2}%
  \makebox[\@tempdima]{\hspace*{#1}#2}%
}
\makeatother
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
Here is some text. \par
Here is \shifttext{20pt}{some} text. \par
Here is \shifttext{-20pt}{some} text. \par
\end{document}

Note that, in its current form, the shifted content shouldn't contain any \label or counter content. However, that can be fixed if needed, by boxing the content first.

1
  • I accepted this answer because it seems like the simplest, it works in LR mode, and it contains an illustration. But the other answers are excellent too. Too bad I can't accept more than one answer. Apr 7, 2012 at 22:37
29

If you are in a vertical context you can use the primitive moveright

\moveright  2in  \hbox{.....}
12
  • 6
    Never heard about \moveright. You never stop learning! Apr 7, 2012 at 20:26
  • @MartinScharrer: It's very rarely used. LaTeX2e uses it in one place (outputting the page headers); L3 nowhere yet. Even ConTeXt doesn't use it. Apr 7, 2012 at 21:00
  • Is there something like this that works in horizontal mode and is already defined in TeX/LaTeX? Or do I have to make my own macro/use a 3rd party package to do it? Apr 7, 2012 at 21:05
  • 2
    @RichardHansen in horizontal mode you can just go \hspace{1in}\mbox{hello}\hspace{-1in} \moveright is the natural analogue of \raisebox which is defined in terns of \raise which works exactly the same way (with the same syntax) as \moveright but in hmode rather than vmode. Apr 7, 2012 at 21:18
  • 1
    no star form would be better despite my comment above, so it works at start of line. Conversly the star form isnt needed if you are already in an hmox, as in the accepted answer. It does no harm to use the star there though:-) Oct 21, 2012 at 7:56
23

My adjustbox package provides \lapbox[<width>]{<amount>}{<content>} which is a generalization of \rlap{<content>} and \llap{<content>}, but can also be used as horizontal equivalent of \raisebox. The <amount> can be positive or negative and will move the content to the right or left, respectively. The original width can be accessed as \width. The official width can be set manually, otherwise it is reduced by the lap amount.

As always there is also a key equivalent for \adjustbox{<key=value>}{<content>} called lap in this case.

Excerpt from the adjustbox manual about \lapbox:

This macro is a generalisation of the LaTeX core macros \rlap{<content>} and \llap{<content>} which lap the text to the right or left without taking any official space. The \lapbox macro can be used to only partially lap the content to the right (positive amount) or left (negative amount). As with all macros of this package the original width can be references using \width. The resulting official width of the box is normally the original width minus the absolute lap amount. However, it can also be set explicitly using the option argument. It is also possible to use lap amount which absolute values are larger than the original width. In this case the resulting official width will be zero by default and the content will padded with the required white space. Note that the lap amount always states the distance between the right side of the official box and the right side of the actual content for positive amounts or the distance between the left side of the official box and the left side of the actual content for negative values.

See the manual for graphical examples.

Example:

If you want to move some content 1em to the right without changing its official width, use:

\usepackage{adjustbox}
% ...
\lapbox[\width]{1em}{Some text}
% or
\adjustbox{lap={\width}{1em}}{Some text}

The \adjustbox version can also be combined with other keys like raise, which is the equivalent of \raisebox.

12

I figured out just another simple alternative, that consists of two boxes, one aligned left and the other right.

\newcommand{\shiftleft}[2]{\makebox[0pt][r]{\makebox[#1][l]{#2}}}
\newcommand{\shiftright}[2]{\makebox[#1][r]{\makebox[0pt][l]{#2}}}

example:

\shiftright{20pt}{text}

Shifts 'text' to the right.

1
  • 1
    Very nice, and much easier to get working than the other answers. Thank you
    – Thomas
    Aug 22, 2015 at 8:33

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.