Tikzset - 绘制线段

Tikzset - 绘制线段

我设置了一个 tikset 来绘制几个线段。基本 pic 命令工作正常,但当我尝试其他命令时,出现了编译错误。

在此处输入图片描述

%%%%%%%%%%%%%%%%%% 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}

有没有人有解决方案

  • \pic {segment={label=B, start=2, end=3}}
  • \pic at (0,2) {segment={label=C, start=1, end=4}}

答案1

如果您添加缺少的分号来终止路径,它们都可以正常工作(从没有错误消息的角度来看)。您需要一个分号,因为\pic开始一个路径,并且在 TiZ 路径将以 终止;

%%%%%%%%%%%%%%%%%% 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}

在此处输入图片描述

答案2

考虑使用理智的蒂克兹。请参阅下面的代码。在此处输入图片描述

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")

生成的 TikZ 代码:

\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}

相关内容