10

(I guess many will view this nitpicking.) How can I replicate the soft-yet-beautiful-looking segment marks of GeoGebra.

enter image description here

\documentclass[tikz,border=10pt]{standalone}
\usepackage{tkz-euclide}
\usetkzobj{all}
\begin{document}
\begin{tikzpicture}
    \coordinate (A) at (0,0);
    \coordinate (B) at (2,4);
    \coordinate (C) at (4,0);

    \draw(A)--(B)--(C)--cycle;
    \tkzMarkSegment[pos=0.5,mark=||](A,B)
    \tkzMarkSegment[pos=0.5,mark=||](B,C)
\end{tikzpicture}
\end{document}

enter image description here

blackened
  • 4,181
  • 2
    Can you explain further about how they differ? I see no (major) difference, but my eye is not good at compare things. –  Mar 02 '19 at 12:20
  • 1
    Related in a very roundabout way: https://tex.stackexchange.com/questions/161297/is-there-such-a-thing-as-a-mathrule-rounded-endcaps – Steven B. Segletes Mar 02 '19 at 14:04

2 Answers2

7

Use decorations.markings to customize the markings as you like.

To control the distance between the two ticks, just modify the x coordinates here:

\draw[line cap=round, thick] (-2pt,-2pt) -- (-2pt,2pt);
\draw[line cap=round, thick] (2pt,-2pt) -- (2pt,2pt);

MWE:

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[decoration={
markings,
mark=at position .5 with
{
\draw[line cap=round, thick] (-2pt,-2pt) -- (-2pt,2pt);
\draw[line cap=round, thick] (2pt,-2pt) -- (2pt,2pt);
}
}
]
    \coordinate (A) at (0,0);
    \coordinate (B) at (2,4);
    \coordinate (C) at (4,0);

    \draw(A)--(B)--(C)--cycle;
    \path[postaction={decorate}] (A) -- (B);
    \path[postaction={decorate}] (B) -- (C);
\end{tikzpicture}
\end{document}

enter image description here

CarLaTeX
  • 62,716
  • @blackened Don't use tikz-euclide but decorations.markings to customize the markings as you like. See my renewed answer. – CarLaTeX Mar 02 '19 at 14:50
7

You could just build your own and then have the full flexibility of controlling every aspect of its appearance.

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}[|| mark/.style={postaction=decorate,decoration={markings,
mark=at position #1 with {\draw[line cap=round,mark segment] (-1pt,-2pt) -- (-1pt,2pt);
\draw[line cap=round,mark segment] (1pt,-2pt) -- (1pt,2pt);
}}},mark segment/.style={thick}]
    \coordinate (A) at (0,0);
    \coordinate (B) at (2,4);
    \coordinate (C) at (4,0);

    \draw(A)--(B)--(C)--cycle;
    \path[|| mark=0.5] (A) -- (B);
    \path[|| mark=0.5] (B) -- (C);
\end{tikzpicture}
\end{document}

enter image description here

enter image description here

A more customizable version thereof is

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{decorations.markings,angles}
\begin{document}
\tikzset{insert |/.style={decoration={markings,
  mark=at position #1 with {%
   \draw[line cap=round,mark segment] 
    (0,-\pgfkeysvalueof{/tikz/mark
    segment length}/2) -- (0,\pgfkeysvalueof{/tikz/mark
    segment length}/2);}
  }},
  | mark/.style={postaction=decorate,insert |=#1},
  insert ||/.style={decoration={markings,
  mark=at position #1 with {%
   \draw[line cap=round,mark segment] 
    (-\pgfkeysvalueof{/tikz/mark segment distance}/2,-\pgfkeysvalueof{/tikz/mark
    segment length}/2) -- (-\pgfkeysvalueof{/tikz/mark segment distance}/2,\pgfkeysvalueof{/tikz/mark
    segment length}/2);
   \draw[line cap=round,mark segment] (\pgfkeysvalueof{/tikz/mark segment distance}/2,-\pgfkeysvalueof{/tikz/mark
    segment length}/2) -- (\pgfkeysvalueof{/tikz/mark segment distance}/2,\pgfkeysvalueof{/tikz/mark
    segment length}/2);}
  }},
  || mark/.style={postaction=decorate,insert ||=#1},
 insert |||/.style={decoration={markings,
  mark=at position #1 with {%
   \draw[line cap=round,mark segment] 
    (-\pgfkeysvalueof{/tikz/mark segment distance},-\pgfkeysvalueof{/tikz/mark
    segment length}/2) -- (-\pgfkeysvalueof{/tikz/mark segment distance},\pgfkeysvalueof{/tikz/mark
    segment length}/2);
   \draw[line cap=round,mark segment] 
    (0,-\pgfkeysvalueof{/tikz/mark
    segment length}/2) -- (0,\pgfkeysvalueof{/tikz/mark
    segment length}/2); 
   \draw[line cap=round,mark segment] 
   (\pgfkeysvalueof{/tikz/mark segment distance},-\pgfkeysvalueof{/tikz/mark
    segment length}/2) -- (\pgfkeysvalueof{/tikz/mark segment distance},\pgfkeysvalueof{/tikz/mark
    segment length}/2);}
  }},
  ||| mark/.style={postaction=decorate,insert |||=#1},
 mark segment/.style={thick},
 mark segment options/.code=\tikzset{mark segment/.style={#1}},
 mark segment distance/.initial=2pt,
 mark segment length/.initial=4pt,
 angle deco |/.style={insert |=0.5,
      pic actions/.append code=\tikzset{postaction=decorate}},
 angle deco ||/.style={insert ||=0.5,
      pic actions/.append code=\tikzset{postaction=decorate}},
 angle deco |||/.style={insert |||=0.5,
      pic actions/.append code=\tikzset{postaction=decorate}}}
\begin{tikzpicture}[]
    \coordinate (A) at (0,0);
    \coordinate (B) at (2,4);
    \coordinate (C) at (4,0);

    \draw(A)--(B)--(C)--cycle;
    \path[| mark=0.5] (A) -- (B);
    \path[mark segment options={thick,yscale=2},|| mark=0.5] (B) -- (C);
    \path[mark segment options={blue,line width=1pt,scale=2},||| mark=0.5] (C) -- (A);
    \pic [draw,angle radius=1cm,angle deco ||] {angle = A--B--C};
\end{tikzpicture}
\end{document}

enter image description here

Explanation: With decoration.markings you switch to the tangent space at a given point, determined by the position. Thus a line in y direction will be orthogonal to the tangent at the curve at the point.

To be moved somewhere else A means to insert anything (like e.g. a diamond).

\documentclass[tikz,border=10pt]{standalone}
\usetikzlibrary{decorations.markings,angles,shapes.geometric,calc}
\begin{document}
\tikzset{insert any/.style={decoration={markings,
  mark=at position #1 with {%
   \path (0,0) node[any mark]{};}
  }},
  diamond mark/.style={postaction=decorate,any mark options=common diamond,insert any=#1},
  insert two/.style={decoration={markings,
  mark=at position #1 with {%
   \path (-\pgfkeysvalueof{/tikz/mark any distance}/2,0) node[any mark]{}
   (\pgfkeysvalueof{/tikz/mark any distance}/2,0) node[any mark]{};}
  }},
  two diamonds mark/.style={postaction=decorate,any mark options=common diamond,,insert two=#1},
 insert three/.style={decoration={markings,
  mark=at position #1 with {%
   \path (-\pgfkeysvalueof{/tikz/mark any distance},0) node[any mark]{}
   (0,0) node[any mark]{}
   (\pgfkeysvalueof{/tikz/mark any distance},0) node[any mark]{};}
  }},
  three diamonds mark/.style={postaction=decorate,any mark options=common diamond,insert three=#1},
 any mark/.style={transform shape},
 any mark options/.code=\tikzset{any mark/.append style={#1}},
  common diamond/.style={shape=diamond,inner  xsep=0.2ex,aspect=0.3,fill},
 mark any distance/.initial=0.6em,}
\begin{tikzpicture}[]
    \coordinate (A) at (0,0);
    \coordinate (B) at (2,4);
    \coordinate (C) at (4,0);

    \draw(A)--(B)--(C)--cycle;
    \path[diamond mark=0.5] (A) -- (B);
    \path[any mark options={aspect=0.5},two diamonds mark=0.5] (B) -- (C);
    \path[any mark options={blue,draw,fill=white},three diamonds mark=0.5] (C) -- (A);
\end{tikzpicture}
\end{document}

enter image description here

  • 5
    I was renewing my answer while you answered, however the answers are slightly different, I would leave them both. – CarLaTeX Mar 02 '19 at 14:54
  • @blackened I added a version in which you can control the parameters with pgfkeys. –  Mar 02 '19 at 15:06
  • @blackened What do you mean by that? Do you want the line caps of angle arcs to be round? –  Mar 08 '19 at 15:31
  • 1
    @blackened Something like https://tex.stackexchange.com/a/411018/121799 ? –  Mar 08 '19 at 15:59
  • 1
    @blackened I added a version that does all that except for the arcs and the dot. (Several users want to have updates to their answers at the same time, so that is the best I can do given my other time constraints.) –  Mar 08 '19 at 16:31
  • @blackened You can always ask, but it is better to ask new questions. Your original question is highly voted, also because it asks for something many may want to use. If the answers get extended by many things, this might not be a good thing. Imagine I add 10 features which have not much to do with the original query, and the 9th feature has a bug. Then everyone just seeking a code that solves the original problem would have to load a lot of overhead, some of which does not work properly. I'd like to avoid that. –  Mar 08 '19 at 16:45
  • @marmot Do I need to ask another question if were to need your help to also add option to have diamond or diamonds markings similar to those in this figure? – blackened May 03 '19 at 06:45
  • @blackened I feel that if you want to insert diamonds, then it is better to write something that allows one to insert anything, including diamonds. I added something that does that but I feel that it deserves a new question since otherwise it is hard for others to find it. Please note that I won't have time to answer such questions in the next 8 hours. –  May 03 '19 at 15:54