Text Formattingsuperscript

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

superscript

Post by Laurentius »

Is there a simple way to create a superscript above a normal letter? F. ex., the ¨ in the German ü is a superscribed e. If one wanted to substitute for this corrupt ¨ the original e, how might one proceed?
Last edited by Laurentius on Mon Jan 03, 2011 10:02 pm, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

superscript

Post by frabjous »

You could use the accents package. It is designed for math mode, but if you escape back to textmode with amsmath's \text command, you can make it work pretty well in text mode. I played around with some ways of doing this using only text mode commands, but wasn't able to do any better, and this seems to work just fine.

A quick example of how you might proceed:

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{accents}

\newcommand{\overaccent}[2]{\ensuremath{\accentset{\text{\tiny #1}}{\text{#2}}}}

\begin{document}

k\overaccent{e}{o}nnen

\end{document}
oe.png
oe.png (1.48 KiB) Viewed 6207 times
If you really want it the size of a regular superscript, you could use \scriptsize instead of \tiny, but I think it looks better with \tiny.
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

superscript

Post by Laurentius »

Thanks! I can't use the accents package, though, it seems to conflict with some other package I am using, one of these,

Code: Select all

\RequirePackage{fontspec} % can be made [quiet].
\RequirePackage{xltxtra} % includes fontspec and xunicode
\RequirePackage{graphicx}
\RequirePackage{soul} % spaces out
\RequirePackage[polutonikogreek,latin,french,danish,german,english]{babel}
\RequirePackage{multicol}
\RequirePackage[commabeforerest,bibformat={compress,ibidem},oxford,ibidem=strict,opcit,titleformat={}]{jurabib}
Did you manage to do it perfectly in text mode? I guess one would have to get the width of the o and the space between and kern-(space+width/2) and raisebox, but I don't know how to get the width.
Last edited by Stefan Kottwitz on Mon Jan 21, 2013 9:45 am, edited 1 time in total.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

superscript

Post by frabjous »

A couple things about your sample -- you shouldn't use \RequirePackage in your document. That's for use inside packages that require other packages. Use \usepackage instead. Also if you're using fontspec and xltxtra, you're using XeLaTeX to compile, in which case you probably shouldn't use babel, but polyglossia, to set up language-specific hyphenation, etc., if at all possible.

It's possible that with some juggling of the order of the packages, you could make the accents package compatible. But it's not worth it. There are many ways to achieve what you want.

You mentioned getting the width of the "o". You can get the width of something with the \settowidth command. (There's also the \widthof command from the calc package, but I prefer \settowidth since it doesn't require any packages.) Actually, combing this with \settoheight gives you a nice albeit complicated way to handle this, something like:

Code: Select all

\documentclass{article}
\usepackage{fontspec} % can be made [quiet].
\usepackage{xltxtra} % includes fontspec and xunicode
\usepackage{graphicx}
\usepackage{soul} % spaces out
\usepackage[polutonikogreek,latin,french,danish,german,english]{babel}
\usepackage{multicol}
\usepackage[commabeforerest,bibformat={compress,ibidem},oxford,ibidem=strict,opcit,titleformat={}]{jurabib}

\newlength{\accentheight}
\newlength{\accentwidth}
    
\newcommand{\overaccent}[2]{%
      \settoheight{\accentheight}{#2}%
      \settowidth{\accentwidth}{#2}%
      \put(0,0){\makebox[\accentwidth]%
          {\raisebox{1.2\accentheight}{\tiny #1}}}%
          #2}

\begin{document}


k\overaccent{e}{o}nnen

\end{document}
You may want to tweak the multiplier before the use of \accentheight for the raisebox to your liking.

But I considered a bunch of other options, all of which work OK; my main reason for preferring that option was these others seem to require more manual tweaking of the vertical placement. Here are three other options. Take your pick.

Using \shortstack:

Code: Select all

\documentclass{article}
\usepackage{fontspec} % can be made [quiet].
\usepackage{xltxtra} % includes fontspec and xunicode
\usepackage{graphicx}
\usepackage{soul} % spaces out
\usepackage[polutonikogreek,latin,french,danish,german,english]{babel}
\usepackage{multicol}
\usepackage[commabeforerest,bibformat={compress,ibidem},oxford,ibidem=strict,opcit,titleformat={}]{jurabib}

\newcommand{\overaccent}[2]{\shortstack{\tiny #1\\#2}}

\begin{document}


k\overaccent{e}{o}nnen

\end{document}
Here's a method using amsmath's \overset command, fairly similar to my first suggestion:

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec} % can be made [quiet].
\usepackage{xltxtra} % includes fontspec and xunicode
\usepackage{graphicx}
\usepackage{soul} % spaces out
\usepackage[polutonikogreek,latin,french,danish,german,english]{babel}
\usepackage{multicol}
\usepackage[commabeforerest,bibformat={compress,ibidem},oxford,ibidem=strict,opcit,titleformat={}]{jurabib}

\newcommand{\overaccent}[2]{\ensuremath{\overset{\text{\tiny #1}}{\text{#2}}}}

\begin{document}

k\overaccent{e}{o}nnen

\end{document}
That definitely might require some vertical fine-tuning.

Finally, you could use a smashed tabular environment with b-placement:

Code: Select all

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec} % can be made [quiet].
\usepackage{xltxtra} % includes fontspec and xunicode
\usepackage{graphicx}
\usepackage{soul} % spaces out
\usepackage[polutonikogreek,latin,french,danish,german,english]{babel}
\usepackage{multicol}
\usepackage[commabeforerest,bibformat={compress,ibidem},oxford,ibidem=strict,opcit,titleformat={}]{jurabib}

\newcommand{\overaccent}[2]{\smash{\begin{tabular}[b]{@{}c@{}} \tiny #1 \\[-1.5ex] #2 \end{tabular}}}

\begin{document}

k\overaccent{e}{o}nnen

\end{document}
Again, more vertical fine tuning may be necessary. Just modify the negative space with the \\.

And probably there are better ways too!
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

superscript

Post by Laurentius »

Thanks alot! Your first solution works very well for me. One tiny problem left: I used

Code: Select all

\catcode`ö=\active
\defö{\overaccent{e}{o}}
but now words beginning with an ö swallows the preceding space. I assume there is a simple, one command solution to this?
Last edited by cgnieder on Mon Jan 21, 2013 1:55 pm, edited 1 time in total.
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

superscript

Post by frabjous »

The problem isn't with your active character definition -- you get the same misbehavior with my definition even if you don't use the active character. I should have tested it more thoroughly. Sorry.

Another makebox in the definition seems to help:

Code: Select all

\newcommand{\overaccent}[2]{%
      \xspace\settoheight{\accentheight}{#2}%
      \settowidth{\accentwidth}{#2}%
      \makebox{\put(0,0){\makebox[\accentwidth]%
          {\raisebox{1.2\accentheight}{\tiny #1}}}%
          #2}}
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Re: superscript

Post by Laurentius »

Brilliant! Thank you.
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Re: superscript

Post by Laurentius »

It messes up the hyphenation, though …
Laurentius
Posts: 132
Joined: Wed Feb 11, 2009 11:38 pm

Re: superscript

Post by Laurentius »

Does anyone have a solution to the hyphenation problem?
Post Reply