3

I'm sure there is already such a question, but after searching I didn't find anything.

I'm looking for the method that would make \sim stretch depending on the text above.

Here is the example of what I need.

enter image description here

antshar
  • 4,238
  • 9
  • 30

3 Answers3

3

The Plain TeX based solution without TikZ:

\newbox\wtilde
\setbox\wtilde=\hbox{%
   \pdfliteral{q 2 0 0 2 0 0 cm}\lower1.5ex\rlap{$\widetilde{\hphantom{xxx}}$}\pdfliteral{Q}%
    \hphantom{xxxxxx}}

$$ \pmatrix{1&1&1&1\cr1&1&1&1\cr1&1&1&1\cr1&1&1&1} \vcenter{\baselineskip=.7\baselineskip\halign{\hfil$\scriptstyle#$\hfil\cr r_2 - r_1 \cr r_3 - 2r_1 \cr \copy\wtilde \cr r_4 - 2r_2 \cr \null \cr }} \pmatrix{1&1&1&1\cr1&1&1&1\cr1&1&1&1\cr1&1&1&1} $$

\bye

enter image description here

Stephen
  • 3,826
wipet
  • 74,238
2

Just for fun: an attempt based on this answer and the calligraphy library by the same author.

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{calc,calligraphy,decorations.pathreplacing}
% based on: https://tex.stackexchange.com/a/15855/194703
\newcommand{\xwidesim}[2][]{%
\setbox0\hbox{$\scriptstyle#2$}%
\setbox1\hbox{$\scriptstyle#1$}%
\mathrel{\begin{tikzpicture}[baseline=0pt] 
\pgfmathsetmacro{\wmax}{max(max(\wd0,\wd1)+4,12)}
\node[above] at (0,{0.2*\wmax*1pt}) (a) {\(\scriptstyle #2\)};
\node[below] at (0,0) (b) {\(\scriptstyle #1\)};
\pen (-135:{min(\wmax,10)*0.1pt}) -- (45:{min(\wmax,10)*0.1pt});
\path (-\wmax*0.5pt-2pt,{0.025*\wmax*1pt+2pt}) coordinate(first)
(\wmax*0.5pt+2pt,{0.025*\wmax*1pt+2pt}) coordinate(last);
\calligraphy[light,line width=1.5pt]
(first) .. controls
 ($($(first)!0.25!(last)$)!{0.1*\wmax+0.4pt}!90:(last)$) 
 .. ($(first)!0.5!(last)$) 
 .. controls ($($(first)!0.75!(last)$)!{0.1*\wmax+0.4pt}!-90:(last)$)
.. (last);   
\end{tikzpicture}}}
\begin{document}
\[
B=\begin{pmatrix}
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
\end{pmatrix}
\xwidesim[r_4-2r_1]{\substack{r_2-r_1\\ r_3-2r_1}}
\begin{pmatrix}
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
\end{pmatrix}
\xwidesim{r}
\begin{pmatrix}
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
 1 & 1 & 1 & 1\\
\end{pmatrix}
\]
\end{document}

enter image description here

2

A tikz attempt (copied from my previous answer on zhihu.com, in Chinese):

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}

\makeatletter
\def\sim@x@scale{.15}
\def\sim@y@scale{.05}
\def\sim@y@thick{.02}

\newsavebox\sim@upper
\newsavebox\sim@lower

% extensible sim symbol
\NewDocumentCommand{\xSim}{ O{} m }{%
  \TextOrMath{%
    \PackageError{TEST}{`\string\xSim` is valid in math mode only.}{}%
  }{
    % math mode only, hence no need to eliminate spaces
    \sbox\sim@upper{$\scriptsize #2$}
    \sbox\sim@lower{$\scriptsize #1$}
    \pgfmathparse{min(max(\wd\sim@upper/1em, \wd\sim@lower/1em, 1.0), 1.5)}
    \edef\sim@ratio{\pgfmathresult}
    \def\sim@x {\sim@x@scale * \sim@ratio}
    \def\sim@y {\sim@y@scale * \sim@ratio}
    \def\sim@@y{\sim@y@thick * \sim@ratio}
    \pgfmathparse{floor(max(\wd\sim@upper/1em, \wd\sim@lower/1em)) + 1}
    \edef\sim@wd{\pgfmathresult em}
    \mathrel{
      \begin{tikzpicture}[baseline=-.7ex]
        \filldraw[line width=.2pt] 
          (0, 0)
            .. controls +(\sim@x, \sim@y+\sim@@y) and +(-\sim@x, -\sim@y) .. 
          +(\sim@wd, 0) 
            node[midway, above] {\usebox\sim@upper} 
            node[midway, below] {\usebox\sim@lower}
            .. controls +(-\sim@x, -\sim@y-\sim@@y) and +(\sim@x, \sim@y) .. 
          (0, 0);
      \end{tikzpicture}
    }
  }%
}
\makeatother

\begin{document}
\foreach \x in {a, aa, aaa, aaaa, aaaaa, aaaaaaaaaaaaaaaaaaa} {
  $\frac12 A \xSim{\x} B \sim C \xSim[\x]{} D$ \par
}
\end{document}

enter image description here

muzimuzhi Z
  • 26,474