18

How can the \sqrt command be modified to split root symbol into two or more lines automatically?

Example:

\documentclass{standalone}

\begin{document}
    \begin{minipage}[t]{0.8\textwidth}
        The \textbf{Euclidean distance} between two points $P=(x, y, z)$and $Q=(a, b, c)$
        in space is defined as $d(P, Q)= \sqrt{(x - a)^{2} + (y - b)^{2} + (z - c)^{2}}$.
        The distance between a point $P$ and a geometric object $S$ like a line or plane is
        the minimal distance $d(P, Q)$ which is possible with $Q$ on $S$.
    \end{minipage}
\end{document}

have

And I want something like this (artificial example using \overline):

\documentclass{standalone}

\begin{document}
    \begin{minipage}[t]{0.8\textwidth}
        The \textbf{Euclidean distance} between two points $P=(x, y, z)$and $Q=(a, b, c)$
        in space is defined as $d(P, Q)= \sqrt{(x - a)^{2} +}$ $\overline{(y - b)^{2} + (z - c)^{2}}$.
        The distance between a point $P$ and a geometric object $S$ like a line or plane is
        the minimal distance $d(P, Q)$ which is possible with $Q$ on $S$.
    \end{minipage}
\end{document}

want

9
  • 6
    Can you not afford a display-math environment in your document? Your square root would be easier to parse in one.
    – jub0bs
    Apr 29 '13 at 13:34
  • 2
    There is something strange with the space between the coordinates x,y,z of the point P.
    – Sigur
    Apr 29 '13 at 13:35
  • @Jubobs: That's intended to be an inline equation.
    – m0nhawk
    Apr 29 '13 at 13:42
  • 1
    You could have provided the codes for the two examples (MWEs), to save people trying to help you some time ;)
    – yo'
    Apr 29 '13 at 13:44
  • 8
    @m0nhawk Don't inflict that to your readers, please.
    – egreg
    Apr 29 '13 at 13:45
15
+50

This version allows TeX to break the math automatically. In this version you can only do this once a full version would need a counter and generate new unique names for the points each time.

enter image description here

\documentclass{article}

\makeatletter
\def\savepos#1{\leavevmode\pdfsavepos\write\@auxout{%
\gdef\string\save@#1{{\the\pdflastxpos sp }{\the\pdflastypos sp }}}}

\def\xx#1{\expandafter\expandafter\expandafter\@firstoftwo\csname save@#1\endcsname}
\def\yy#1{\expandafter\expandafter\expandafter\@secondoftwo\csname save@#1\endcsname}


\def\xsqrt#1{%
\sqrt{\vphantom{#1}}%
\ifx\save@L\@undefined
\else
\ifdim\yy{L}=\yy{R}%
\else
\rlap{$\overline{\vphantom{#1}\hskip\dimexpr\xx{b}-\xx{L}\relax}$}%
\fi
\fi
\savepos{L}#1\savepos{R}%
\ifx\save@L\@undefined
\else
\ifdim\yy{L}=\yy{R}%
\llap{$\overline{\hskip\dimexpr\xx{R}-\xx{L}\relax}$}%
\else
\llap{$\overline{\vphantom{#1}\hskip\dimexpr\xx{R}-\xx{a}\relax}$}%
\fi
\fi
}

\makeatother

\begin{document}



    \savepos{a}\begin{minipage}[t]{0.8\textwidth}
        The \textbf{Euclidean distance} between two points $P=(x, y, z)$ and $Q=(a, b, c)$
        in space is defined as $d(P, Q)= \xsqrt{(x - a)^{2} +(y - b)^{2} + (z - c)^{2}}$.
        The distance between a point $P$ and a geometric object $S$ like a line or plane is
        the minimal distance $d(P, Q)$ which is possible with $Q$ on $S$. 
    \end{minipage}\savepos{b}
\end{document}
3
  • 1
    You are a wizard? This is fantastic! Apr 29 '13 at 21:43
  • You have a little bug in your code: when \yy{L}=\yy{R} then the \vphantom{} is missing in the code. And another problem: the \sqrt mark have discrete sizes, maybe it doesn't vertically fit with \overline (try to use \bigl( inside the formula). Better would be: \def\myoverline#1#2{\vbox{\hrule\kern-.4pt\hbox{$\vphantom{#1}\hskip#2$}}}.
    – wipet
    Apr 17 '15 at 16:50
  • @wipet thanks yes probably, looks about right (once I'd remembered what this answer was doing at all:-) Apr 17 '15 at 16:55
18

In cases such as the one you've brought up, I believe it's both simpler (for you) and clearer (for your readers) if you don't use the "surd" symbolism at all. Instead, use [...]^{1/2}, as is done in the following modified form of your MWE. Alternatively, as others have already suggested as well, you could move the long term into a "displayed" equation, i.e., onto a line by itself.

Not only is [...]^{1/2} simpler for you (because you don't have concern yourself with where the line break may fall), it is also easier on the readers' eyes (i.e., less distracting and hence also clearer) since the interline spacing in the paragraph in question doesn't have to be adjusted to accommodate the "surd".

enter image description here

\documentclass{standalone}
\begin{document}
\begin{minipage}{0.8\textwidth}
The \textbf{Euclidean distance} between two points $P=(x, y, z)$ and 
$Q=(a, b, c)$ in space is defined as $d(P, Q)= [(x - a)^{2} +(y - b)^{2} 
+ (z - c)^{2}]^{1/2}$. The distance between a point $P$ and a geometric 
object $S$, such as a line or plane, is the minimal distance $d(P, Q)$ 
which is possible with $Q\in S$.
\end{minipage}
\end{document}
2
  • Just a typo: 1/2.
    – Sigur
    Apr 29 '13 at 14:13
  • 1
    @Sigur - thanks, I've fixed the typo
    – Mico
    Apr 29 '13 at 14:16
6

enter image description here

\documentclass[preview,border=12pt]{standalone} % change it to your class!

\usepackage{mathtools}


\begin{document}
\begin{multline*}
A   = \sqrt{a+b+c} \\
    \overline{\rule{0pt}{2.5ex}+d+e+f}\\
    \overline{\rule{0pt}{2.5ex}+g+h+i}
\end{multline*}
\end{document}

Just another example:

enter image description here

\documentclass[preview,border=12pt]{standalone}% change it to your own document class!
\usepackage[a6paper,margin=1cm]{geometry}
\usepackage{mathtools}


\begin{document}
\begin{multline*}
d(x,y)  
    = \sqrt{(x-x_0)^2} \\
        \overline{\rule{0pt}{2.5ex}{}+(y-y_0)^2}\\
        \overline{\rule{0pt}{2.5ex}{}+(z-z_0)^2}
\end{multline*}
\end{document}
2
  • It is not so easy to handle with \rule{0pt}{2.5ex}.
    – Sigur
    Apr 29 '13 at 13:41
  • 7
    It seems to me that this is the same manual tweak as what m0nhawk does in the question. The problem is how to make it automated.
    – yo'
    Apr 29 '13 at 13:43
4

Here is a simple solution. When you need, just use the new command and split the radical as you want.

\newcommand{\sqrtx}[2]{\sqrt{#1}\\\overline{#2}}

here some text \dotfill$\sqrtx{x^2+y^2}{z^2+y^2}$

enter image description here

5
  • 4
    Although, it would be better to have TeX decided where this break should take place. Apr 29 '13 at 14:09
  • @SeanAllred but TeX's aesthetic judgement would say never break here, so leaving it for TeX to decide can't really work:-) Apr 29 '13 at 14:26
  • @DavidCarlisle Points, you have good ones, but is it not possible to `trick' TeX? Apr 29 '13 at 14:29
  • 2
    @SeanAllred anything is possible Apr 29 '13 at 14:31
  • 1
    @SeanAllred see another answer:-) Apr 29 '13 at 21:30

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.