3

I need to put a symbol over some text, I want to use \hspace{#1} where #1 is the width of the #1 text used.

For example, write \mycommand{p}{b} and the result to be shown like the b over the p, just like the symbol \th, but allowing me to write more than one letter.

For example, \mycommand{text1}{\LaTeX}, placing the word \LaTex over the word text

2
  • Perhaps it's better if you tell exactly what you have in mind: I think that \hspace is not what you need. Probably there are better ways.
    – egreg
    Jul 29, 2014 at 21:53
  • Perhaps \settowidth{\mylen}{Some text} is what you want (see here). The command as given sets \mylen to the width of the 'Some text'.
    – jon
    Jul 29, 2014 at 22:45

2 Answers 2

4

A solution based on the stackengine package. I define a \superimpose command with two mandatory arguments and one optional (the alignment: r, l or c — the default):

    \documentclass[a4paper]{book}
    \usepackage[utf8]{inputenc}
    \usepackage{xcolor} 
    \usepackage{etoolbox} 
    \usepackage{stackengine}
    \newcommand\superimpose[3][]{%
    \def\stackalignment{#1}
    \stackon[0pt]{\smash{#2}}{#3}\vphantom{#2}\def\stackalignment{c}}

    \begin{document}

    \superimpose{\LaTeX}{\color{red}text}

    \superimpose[l]{\LaTeX}{\color{green}text}

    \superimpose[r]{\LaTeX}{\color{blue}text}

    \end{document} 

compiled example

8

If you want to measure the width of a text, you can do by using a length with

\newlength{\mylen}

in the preamble and then

\settowidth{\mylen}{text to measure}

when you need it; you can use \mylen in the argument of \hspace, for instance.

I'm not sure what this is for, but here's how you can do it without any measuring.

\documentclass{article}

\newcommand{\overwrite}[2]{%
  {\ooalign{\hfil#1\hfil\cr\hfil#2\hfil\cr}}\vphantom{#1#2}}

\begin{document}
\overwrite{p}{b}

\overwrite{text}{\LaTeX}
\end{document}

No need to measure things, as TeX knows how to do it.

enter image description here

2
  • It worked! Anyway, I would like to know how to set a the word length as a variable.
    – hkviktor
    Jul 29, 2014 at 22:27
  • @hkviktor I added it.
    – egreg
    Jul 29, 2014 at 22:45

You must log in to answer this question.

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