1

I set up a tikset to draw several segments. The basic pic command work properly but when i tried the others I've got compilation errors.

enter image description here

%%%%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%%%
\documentclass[border=10pt]{standalone}

%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{tikz}%  permet de dessiner des figures, des graphiques
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre

%%  FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}%  Copie-colle la police active dans \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)

%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{pictparam/.style={font={\sansmath\sffamily}, thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm, scale=1}}

% segment
%
\tikzset{
    pics/segment/.style={
        code={
            \tikzset{segment/.cd,#1}
            \def\pv##1{
                \pgfkeysvalueof{/tikz/segment/##1}
            }
            \draw[|-|, pic actions] (0,0) coordinate (o) 
            node[anchor=east] {\pv{label}}% Label à gauche
            node[inner sep=9pt, anchor=south, scale=0.75] {\pv{start}} 
            -- ++(-\pv{start}+\pv{end},0) coordinate (b) 
            node[inner sep=9pt, anchor=south, scale=0.75] {\pv{end}};
    }
    },
    segment/.cd,
    label/.initial=A,
    start/.initial=0,
    end/.initial=5,
}
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
\begin{adjustbox}{width={15cm},totalheight={15cm},keepaspectratio}
\begin{tikzpicture}[pictparam]
    \pic {segment}
    %\pic {segment={label=B, start=2, end=3}}%  Doesn't work
    %\pic at (0,2) {segment={label=C, start=1, end=4}}% Doesn't work
\end{tikzpicture}
\end{adjustbox}
\end{document}

Does anyone have an solution for

  • \pic {segment={label=B, start=2, end=3}}
  • \pic at (0,2) {segment={label=C, start=1, end=4}}
  • 1
    They all work if you add ; at the end, e.g. \pic {segment={label=B, start=2, end=3}}; with the semicolon at the end. – Schrödinger's cat Feb 24 at 15:31
3

All of them work (in the sense that there are no error messages) if you add the missing semicolons to terminate the paths. You need a semicolon since \pic starts a path, and in TikZ paths are to be terminated by a ;.

%%%%%%%%%%%%%%%%%% INTRODUCTION %%%%%%%%%%%%%%%%%%
\documentclass[border=10pt]{standalone}

%%%%%%%%%%%%%%%%%% PACKAGE %%%%%%%%%%%%%%%%%%
\usepackage{tikz}%  permet de dessiner des figures, des graphiques
\usepackage{adjustbox}% permet de déterminer une taille de fenêtre

%%  FONT
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tgadventor}% paquet de police de caractère TGadventor
\usepackage{sansmath}%  Copie-colle la police active dans \sfdefault (/!\ N'EST PAS UNE POLICE DE CARACTÈRES)

%%%%%%%%%%%%%%%%%% SETUP %%%%%%%%%%%%%%%%%%
\tikzset{pictparam/.style={font={\sansmath\sffamily}, thick, line cap=round, line join=round, >=latex, x=1.0cm, y=1.0cm, scale=1}}

% segment
%
\tikzset{
    pics/segment/.style={
        code={
            \tikzset{segment/.cd,#1}
            \def\pv##1{
                \pgfkeysvalueof{/tikz/segment/##1}
            }
            \draw[|-|, pic actions] (0,0) coordinate (o) 
            node[anchor=east] {\pv{label}}% Label à gauche
            node[inner sep=9pt, anchor=south, scale=0.75] {\pv{start}} 
            -- ++(-\pv{start}+\pv{end},0) coordinate (b) 
            node[inner sep=9pt, anchor=south, scale=0.75] {\pv{end}};
    }
    },
    segment/.cd,
    label/.initial=A,
    start/.initial=0,
    end/.initial=5,
}
%%%%%%%%%%%%%%%%%% DOCUMENT %%%%%%%%%%%%%%%%%%
\begin{document}
\begin{adjustbox}{width={15cm},totalheight={15cm},keepaspectratio}
\begin{tikzpicture}[pictparam]
    \pic {segment};
    \pic at (0,-1)  {segment={label=B, start=2, end=3}};% 
    \pic at (0,-2) {segment={label=C, start=1, end=4}};% Doesn't work
\end{tikzpicture}
\end{adjustbox}
\end{document}

enter image description here

| improve this answer | |
  • I hate myself so much... thanks for the help – Nilcouv Feb 25 at 11:17
0

Consider using sane_tikz. See below for the code. enter image description here

import sane_tikz as stz

label_spacing = 0.25
tick_label_spacing = 0.25
tick_length = 0.25
segment_spacing = 0.6
length_multiplier = 0.8


def segment(length, label_str, left_tick_label_str, right_tick_label_str):
    seg = stz.line_segment([0, 0], [length, 0])
    left_tick = stz.centered_vertical_line_segment([0, 0], tick_length)
    right_tick = stz.centered_vertical_line_segment([length, 0], tick_length)
    left_tick_label = stz.latex([0, 0], left_tick_label_str)
    right_tick_label = stz.latex([0, 0], right_tick_label_str)
    stz.place_above_and_align_to_the_center(left_tick_label, left_tick,
                                            tick_label_spacing)
    stz.place_above_and_align_to_the_center(right_tick_label, right_tick,
                                            tick_label_spacing)
    seg_label = stz.latex([-label_spacing, 0], label_str)
    return [
        seg, left_tick, right_tick, left_tick_label, right_tick_label, seg_label
    ]


segs = [
    segment(length_multiplier * 5, "A", "0", "5"),
    segment(length_multiplier * 1, "B", "2", "3"),
    segment(length_multiplier * 3, "C", "1", "4")
]

stz.distribute_vertically_with_spacing(segs[::-1], segment_spacing)
stz.draw_to_tikz_standalone(segs, "segments.tex")

Generated TikZ code:

\documentclass{standalone}
\usepackage[T1]{fontenc}\usepackage{tikz}
\usepackage{amsmath, amsfonts}
\usetikzlibrary{arrows.meta}
\begin{document}
\begin{tikzpicture}
\draw[] (0.000000, 2.200000) -- (4.000000, 2.200000);
\draw[] (0.000000, 2.325000) -- (0.000000, 2.075000);
\draw[] (4.000000, 2.325000) -- (4.000000, 2.075000);
\node[] at (0.000000,2.575000) {0};
\node[] at (4.000000,2.575000) {5};
\node[] at (-0.250000,2.200000) {A};
\draw[] (0.000000, 1.100000) -- (0.800000, 1.100000);
\draw[] (0.000000, 1.225000) -- (0.000000, 0.975000);
\draw[] (0.800000, 1.225000) -- (0.800000, 0.975000);
\node[] at (0.000000,1.475000) {2};
\node[] at (0.800000,1.475000) {3};
\node[] at (-0.250000,1.100000) {B};
\draw[] (0.000000, 0.000000) -- (2.400000, 0.000000);
\draw[] (0.000000, 0.125000) -- (0.000000, -0.125000);
\draw[] (2.400000, 0.125000) -- (2.400000, -0.125000);
\node[] at (0.000000,0.375000) {1};
\node[] at (2.400000,0.375000) {4};
\node[] at (-0.250000,0.000000) {C};
\end{tikzpicture}
\end{document}
| improve this answer | |

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.