21

I want to give a good visualization of two functions on the integers. Is there a way to write something that looks like this:

enter image description here

I thought of using tikz, but I am not sure how.

Spenser
  • 361

5 Answers5

24

No need of TikZ for this :-)

\documentclass[10pt]{article}
\usepackage{mathtools}
\usepackage{array}

\newcommand\UpArr[1][\sigma]{%
\begin{matrix}
  \rlap{\hspace{0.6em}%
  \raisebox{-.6\height}{$\mathclap{\downarrow}$}\rule[0.6ex]{3.05em}{0.4pt}\raisebox{-.6\height}{$\mathclap{\downarrow}$}}%
  \end{matrix}%
  \rlap{\raisebox{1.5ex}{\makebox[4.2em][c]{$#1$}}}%
}
\newcommand\DownArr[1][\tau]{%
\begin{matrix}
  \rlap{\hspace{0.6em}%
  \raisebox{\depth}{$\mathclap{\uparrow}$}\rule{3.05em}{0.4pt}\raisebox{\depth}{$‌​\mathclap{\uparrow}$}}%
  \end{matrix}%
  \rlap{\raisebox{-2ex}{\makebox[4.2em][c]{$#1$}}}%
}

\begin{document}

\[
\begin{array}{c*{11}{>{$\hfil}p{2em}<{\hfil$}}c}
& & \UpArr & & \UpArr & & \UpArr & & \UpArr & & \UpArr \\
\cdots & -5 & -4 & -3 & -2 & -1 & \phantom{-}0 & \phantom{-}1 & \phantom{-}2 & \phantom{-}3 & \phantom{-}4 & \phantom{-}5 & \cdots \\
& \DownArr & & \DownArr & & \DownArr & & \DownArr & & \DownArr \\
\end{array}
\]

\end{document}

enter image description here

Moriambar
  • 11,466
Gonzalo Medina
  • 505,128
  • using the \mathclap of mathtools on the up and down arrows there would be no need for the \mkern-5mu which looks like it was obtained by hands, experimentally. –  Sep 13 '13 at 17:34
  • 1
    i.e.: \raisebox{\depth}{$\mathclap{\uparrow}$}\rule{3.05em}{0.4pt}\raisebox{\depth}{$\mathclap{\uparrow}$} which needs no manual adjustment. And \raisebox{-\height}{$\mathclap{\downarrow}$}\rule[-0.4pt]{3.05em}{0.4pt}\raisebox{-\height}{$\mathclap{\downarrow}$}. –  Sep 13 '13 at 17:40
  • Edit: my last suggestion better perhaps with \raisebox{2ex}{the whole thing with \downarrow}. –  Sep 13 '13 at 17:46
  • @jfbu thanks for your suggestions! I've incorporated them in my updated answer. – Gonzalo Medina Sep 13 '13 at 23:42
23

Another option

\documentclass[tikz]{standalone}%

\begin{document}
\begin{tikzpicture}
% the loop runs over the to-be-displayed items
%     \x  : Holds the text
%     \xi : Counts the number of spins (starting from 1)
%     \xj : Holds the previous spin number
\foreach \x[count=\xi,evaluate=\x as \xj using {int(\xi-1)}] in {\dots,-5,-4,...,5,\dots}{
% Place a node with the name (n-<spin no>) and with the text in mathmode.
\node (n-\xi) at (0.8*\xi,0) {$\scriptstyle\x$};
% We want to draw backwards so we need to start from -4 which is the third node
% Test if we have passed the initial ... and -5
\ifnum\xi>2\relax   %  Without \relax TeX keeps on parsing numbers 
  \ifnum\xi<12\relax%  until it encounters something that doesn't look like a number
                    %  it's not necessary here (\ifnum) is one of those things and a
                    %  comment is too small to explain it :P Please search main site for it 
% Now we alternate up and down. This alternating can be smaller, say, only the "above"
% and below text and the coordinate is changed instead of the whole \draw.... Simply 
%  we draw from the current \xi'th node to the previous \xj'th one.
    \ifodd\x
    \draw[<->] (n-\xi) |- ++(-0.4,0.4) node[above]{$\sigma$} -| (n-\xj) ;
    \else
    \draw[<->] (n-\xi) |- ++(-0.4,-0.4)  node[below]{$\tau$} -| (n-\xj);
    \fi
  \fi
\fi
% Close all the if cases
}
\end{tikzpicture}
\end{document}

enter image description here

Now I see that it should have been \ifnum<13 which is an evidence of the shortcoming that it's better check the last value of the list instead of hardcoding it.

percusse
  • 157,807
  • Would you please explain your code? Especially the foreach loop. Why do you relax if xi has a value higher 2 and lower 12? – schmendrich Sep 13 '13 at 07:54
  • @schmendrich It's just precaution to make TeX stop looking for a number further than the numbers to be compared. Here they are not relevant but a coding habit. I'll comment more soon when I have a little time. – percusse Sep 13 '13 at 08:00
  • 1
    @schmendrich Done. – percusse Sep 13 '13 at 10:11
15

A more flexible solution using the chains library. My paths.ortho library may help with the ud and du to paths.

Code

\documentclass[tikz]{standalone}
\usetikzlibrary{chains}
\makeatletter
\tikzset{
 edge node/.code={\expandafter\def\expandafter\tikz@tonodes\expandafter{\tikz@tonodes#1}},
 empty edge nodes/.code={\let\tikz@tonodes\pgfutil@empty},
 integer function/.code={%
    \tikzset{#1=of \tikzchainprevious}%
    \ifodd\tikzchaincount
      \tikzset{join=by {every odd integer function/.try={#1},
                        integer function \tikzchaincount/.try={#1}}}%
    \else
      \tikzset{join=by {every even integer function/.try={#1},
                        integer function \tikzchaincount/.try={#1}}}%
    \fi}}
\makeatother
\tikzset{
  uddu distance/.initial=.25cm,
  ud/.style={to path={
    -- ([yshift=\pgfkeysvalueof{/tikz/uddu distance}] \tikztostart.north)
    -- ([yshift=\pgfkeysvalueof{/tikz/uddu distance}] \tikztotarget.north) \tikztonodes
    -- (\tikztotarget)}},
  du/.style={to path={
    -- ([yshift=-\pgfkeysvalueof{/tikz/uddu distance}] \tikztostart.south)
    -- ([yshift=-\pgfkeysvalueof{/tikz/uddu distance}] \tikztotarget.south) \tikztonodes
    -- (\tikztotarget)}},
  every odd integer function/.style={ud, edge node={node[every odd node/.try]{$\sigma$}}},
  every even integer function/.style={du, edge node={node[every even node/.try]{$\tau$}}},
  every odd node/.style={midway, above},
  every even node/.style={midway, below}
}
\begin{document}
\begin{tikzpicture}[
  node distance=+.5em,
  text depth=+0pt,
  every join/.append style={<->},
  start chain=ch going {integer function=right},
  integer function 3/.style={bend left=90},
  integer function 5/.style={
    empty edge nodes, edge node={node [above] {$\sigma_5$}}},
  integer function 8/.style={blue},
  ]
\foreach \cnt in {-5, ..., 5}
  \node[on chain=ch, text width=width("$-0$"), align=center] {$\cnt$};

\node[left=of ch-begin] {$\cdots$}; \node[right=of ch-end]  {$\cdots$};
\end{tikzpicture}
\end{document}

Output

enter image description here

Qrrbrbirlbel
  • 119,821
10

With PSTricks and a simple algorithm to follow.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\psset{arrows=<->,nodesep=6pt}

\begin{document}
\begin{pspicture}(-6,-1)(6,1)
    \foreach \x in {-6,6}{\rput(\x,0){$\cdots$}}
    \foreach \x in {-5,-4,...,5}{\rput(\x,0){$\x$}}
    \foreach \x in {-4,-2,...,4}{\pcbar[angle=90](\x,0)(!\x\space 1 add 0)\naput{$\sigma$}}
    \foreach \x in {-5,-3,...,3}{\pcbar[angle=-90](\x,0)(!\x\space 1 add 0)\nbput{$\tau$}}
\end{pspicture}
\end{document}

enter image description here

6

enter image description here

MWE with Asymptote:

% fint.tex:
\documentclass{article}
\usepackage[inline]{asymptote}
\usepackage{lmodern}
\begin{document}
\begin{asy}
unitsize(20bp);
import roundedpath; defaultpen(fontsize(10pt));
int n=6; real dx=1.2,dy=0.9, hl=0.2, hh=0.8;
guide arsig=roundedpath((dx,dy*hl)--(dx,dy*hh)--(0,dy*hh)--(0,dy*hl),0.2);
guide artau=rotate(180)*arsig;
pen textPen=darkblue, sigPen=blue+0.8bp, tauPen= red+0.8bp;

void draw(int i,guide g,pen p){draw(shift((2i-n+1)*dx,0)*g,p,Arrows(HookHead,size=5,Fill));}

label("\textbf{\dots}",(-(2n-n+1)*dx,0));
for(int i=0;i<n;++i){
  label("$"+string(2i-n)+"$",((2i-n)*dx,0),textPen);
  label("$"+string(2i-n+1)+"$",((2i-n+1)*dx,0),textPen);
  label("$\sigma$",((2i-n+1.5)*dx,dy),sigPen);  
  label("$\tau$",((2i-n+0.5)*dx,-dy),tauPen);  
  draw(i,arsig,sigPen);
  draw(i,artau,tauPen);
}
label("$"+string(2n-n)+"$",((2n-n)*dx,0));
label("\textbf{\dots}",((2n-n+1)*dx,0));
shipout(bbox(Fill(rgb(1,1,0.5))));
\end{asy}
\end{document}
%
%% Process:
%
% pdflatex fint.tex 
% asy -f pdf fint-*.asy     
% pdflatex fint.tex
g.kov
  • 21,864
  • 1
  • 58
  • 95