6

I was wondering if there is a standardized and fancy way to have sliders like these horizontal linear ones in a LaTeX article. Anyone has an idea on how I should start tackling it? I'd like to get an horizontal slider that takes text space, not floating. So far I don't get past this, I don't know how to place a square+triangle shape on top of it:

 \documentclass{article}
 \usepackage{xcolor}
 \usepackage{tikz}
 \begin{document}
 SLIDER \tikz{\path[draw=lightgray,fill=lightgray, rounded corners] (0,0) rectangle (2.5cm,0.2cm);}
 \end{document}
5
  • do you just want to draw them or do you want them to do something if slid (in pdf browsers that could offer such a thing?) May 30, 2014 at 19:49
  • just to draw them, in an article that is going to be just a pdf file
    – DaniCee
    May 30, 2014 at 19:57
  • what would be great is that the position of the triangle (or circle) would be given in a comprehensible way, like percentage or something, is there a way to do that?
    – DaniCee
    May 30, 2014 at 20:00
  • OK please make your example a complete document, so people can run it May 30, 2014 at 20:11
  • Oh sorry, editing now
    – DaniCee
    May 30, 2014 at 20:22

2 Answers 2

13

First attempt:

\usetikzlibrary{calc}

\def\SLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
 \coordinate (start) at (0,0);
 \coordinate (end) at (#1,0);
 \coordinate (mark) at ($(start)!#2!(end)$);
 \draw[line width=0.2cm, line cap=round, lightgray] (start) -- (end);
 \shade[ball color=blue!30!cyan] (mark) circle(0.15cm);
}
}

SLIDER \SLIDER{2.5cm}{0.7}

Result

Update: second attempt

Emboss shadow effect in the bar, more types or markers:

\usetikzlibrary{calc}


\def\CircleSLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
 \coordinate (start) at (0,-0.1cm);
 \coordinate (end) at (#1,0.1cm);
 \coordinate (mark) at ($(start|-0,0)!#2!(end|-0,0)$);
 \fill[rounded corners=0.1cm, draw=gray, bottom color=lightgray, top color=black, middle color=lightgray] (start) rectangle (end);
 \shade[draw=darkgray, rounded corners=0.2mm, ball color=blue!20!cyan] (mark) circle(.15) ;
}
}

\def\SquareSLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
 \coordinate (start) at (0,-0.1cm);
 \coordinate (end) at (#1,0.1cm);
 \coordinate (mark) at ($(start|-0,0)!#2!(end|-0,0)$);
 \fill[rounded corners=0.1cm, draw=gray, bottom color=lightgray, top color=black, middle color=lightgray] (start) rectangle (end);
 \shade[draw=darkgray, rounded corners=0.2mm, ball color=blue!20!cyan] (mark) +(-.15,-.15) rectangle +(.15, .15) ;
}
}
\def\TriangleSLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
 \coordinate (start) at (0,-0.1cm);
 \coordinate (end) at (#1,0.1cm);
 \coordinate (mark) at ($(start|-0,0)!#2!(end|-0,0)$);
 \fill[rounded corners=0.1cm, draw=gray, bottom color=lightgray, top color=black, middle color=lightgray] (start) rectangle (end);
 \shade[draw=darkgray, rounded corners=0.2mm, ball color=blue!20!cyan] (mark) ++(-.15,-.15) -- ++(.15,-.15) -- ++(.15,.15) -- ++(0,.3) -- ++(-.3,0) -- cycle ;
}
}

\CircleSLIDER{2.5cm}{0.4}

\SquareSLIDER{2.5cm}{0.7}

\TriangleSLIDER{2.5cm}{0.3}

Result2

Update 2

Now that shadows, gradients, and reflections are considered outrée in UI design, may be you prefer this version:

\usetikzlibrary{calc}


\def\CircleSLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
 \coordinate (start) at (0,-0.1cm);
 \coordinate (end) at (#1,0.1cm);
 \coordinate (mark) at ($(start|-0,0)!#2!(end|-0,0)$);
 \fill[rounded corners=0.1cm, draw=gray, fill=lightgray] (start) rectangle (end);
 \fill[draw=gray, rounded corners=0.2mm, fill=blue!20!cyan] (mark) circle(.15) ;
}
}

\def\SquareSLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
 \coordinate (start) at (0,-0.1cm);
 \coordinate (end) at (#1,0.1cm);
 \coordinate (mark) at ($(start|-0,0)!#2!(end|-0,0)$);
 \fill[rounded corners=0.1cm, draw=gray,fill=lightgray] (start) rectangle (end);
 \fill[draw=gray, rounded corners=0.2mm, fill=blue!20!cyan] (mark) +(-.15,-.15) rectangle +(.15, .15) ;
}
}
\def\TriangleSLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
 \coordinate (start) at (0,-0.1cm);
 \coordinate (end) at (#1,0.1cm);
 \coordinate (mark) at ($(start|-0,0)!#2!(end|-0,0)$);
 \fill[rounded corners=0.1cm, draw=gray, fill=lightgray] (start) rectangle (end);
 \fill[draw=gray, rounded corners=0.2mm, fill=blue!20!cyan] (mark) ++(-.15,-.15) -- ++(.15,-.15) -- ++(.15,.15) -- ++(0,.3) -- ++(-.3,0) -- cycle ;
}
}

Result3

Update 3

Last one, I promise :-)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,shadows,shadows.blur}

\def\IosSevenSlider#1#2{
    \tikz[baseline=-0.1cm]{
        \coordinate (start) at (0,0);
        \coordinate (end) at (#1,0);
        \coordinate (mark) at ($(start)!#2!(end)$);
        \useasboundingbox (start|- 0,-.25) rectangle (end|- 0, .25);
        \draw[line width=0.4mm, line cap=round, blue!50!cyan] 
             (start) -- (mark) edge[lightgray] (end);
        \node[fill=white, draw=lightgray, very thin,
            blur shadow={shadow xshift=0pt, shadow opacity=20, shadow yshift=-0.9mm,
                         shadow blur steps=6, shadow blur radius=0.3mm},
            circle, minimum size=0.25cm, inner sep=0pt] at (mark) {};
    }
}


\begin{document}
\setlength{\fboxsep}{0pt}
\fbox{\IosSevenSlider{2.5cm}{0.7}}
\end{document}

Result4

4
  • amazing response! exactly what I wanted. The simplest circle design at the end is the most beautiful, indeed. Thanks!
    – DaniCee
    May 30, 2014 at 22:12
  • If the last one is less outrée then I really don't respect UI people anymore :P
    – percusse
    May 30, 2014 at 23:46
  • What is the magic about 0.1cm?
    – Aditya
    May 31, 2014 at 0:50
  • @Aditya In the first and second solution, since the width of the bar is 0.2cm and the origin is in the horizontal axis of the bar, setting a baseline of 0.1cm enforces that the baseline is at the bottom edge of the bar. For the third solution it does not have a rational explanation... perhaps -0.5ex would have more sense (so that the thin bar is centered at the height of an "x")
    – JLDiaz
    May 31, 2014 at 12:08
3

Basedon JLDiaz first attempt:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{calc}

\def\SLIDER#1#2{% 1: length, 2: position of the mark (0 to 1)
\tikz[baseline=-0.1cm]{
    \coordinate (start) at (0,0);
    \coordinate (end) at (#1,0);
    \coordinate (mark) at ($(start)!#2!(end)$);
    \draw[line width=0.2cm, line cap=round, lightgray] (start) -- (end);
    \path[
        fill=blue!30!cyan,
    ] (mark) + (-.15cm, -.1cm) -- +(0cm, -.2cm) -- +(.15cm, -.1cm) -- +(.15cm, .2cm) -- +(-.15cm, .2cm) -- cycle;
}}

SLIDER \SLIDER{2.5cm}{0.7}
\end{document}

2
  • 1
    Can you please complete your document such that when copy/pasted it is compilable? See @JLDiaz' answer.
    – percusse
    May 30, 2014 at 23:47
  • Just added it..
    – Pablo
    May 31, 2014 at 0:16

You must log in to answer this question.

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