Tikz 中创建尺寸线的文档命令出现问题

Tikz 中创建尺寸线的文档命令出现问题

获胜答案Tikz 尺寸遗憾的是,它不适用于 LaTeX3。例如,请看以下内容。

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usetikzlibrary{calc}

%% Dimension Code %%
\tikzset{%
    Cote node/.style={%
        midway,
        sloped,
        fill=white,
        inner sep=1.5pt,
        outer sep=2pt
    },
    Cote arrow/.style={%
        <->,
        >=latex,
        very thin
        }
    }

\makeatletter
\NewDocumentCommand{\Cote}{%
    s       % cotation avec les flèches à l'extérieur
    D<>{1.5pt} % offset des traits
    O{.75cm}    % offset de cotation
    m       % premier point
    m       % second point
    m       % étiquette
    D<>{o}  % () coordonnées -> angle
            % h -> horizontal,
            % v -> vertical
            % o or what ever -> oblique
    O{}     % parametre du tikzset
    }{%

{\tikzset{#8}

\coordinate (@1) at #4 ;
\coordinate (@2) at #5 ;

\if #7v % Cotation verticale
    \coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ; 
    \coordinate (@4) at (@0|-@1) ;
    \coordinate (@5) at (@0|-@2) ;
\else
\if #7h % Cotation horizontale
    \coordinate (@0) at ($($#4!.5!#5$) + (0,#3)$) ; 
    \coordinate (@4) at (@0-|@1) ;
    \coordinate (@5) at (@0-|@2) ;
\else % cotation encoche
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
    \coordinate (@5) at ($#7!#3!#5$) ;
    \coordinate (@4) at ($#7!#3!#4$) ;
\else % cotation oblique    
    \coordinate (@5) at ($#5!#3!90:#4$) ;
    \coordinate (@4) at ($#4!#3!-90:#5$) ;
\fi\fi\fi

\draw[very thin,shorten >= #2,shorten <= -2*#2] (@4) -- #4 ;
\draw[very thin,shorten >= #2,shorten <= -2*#2] (@5) -- #5 ;

\IfBooleanTF #1 {% avec étoile
\draw[Cote arrow,-] (@4) -- (@5)
    node[Cote node] {#6\strut};
\draw[Cote arrow,<-] (@4) -- ($(@4)!-6pt!(@5)$) ;   
\draw[Cote arrow,<-] (@5) -- ($(@5)!-6pt!(@4)$) ;   
}{% sans étoile
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
    \draw[Cote arrow] (@5) to[bend right]
        node[Cote node] {#6\strut} (@4) ;
\else
\draw[Cote arrow] (@4) -- (@5)
    node[Cote node] {#6\strut};
\fi
}}
}
\makeatother

%% Document %%
\begin{document}
\begin{tikzpicture}
\small
\draw[thick,blue,fill=blue!25]
        (0,1) coordinate (A)
    --  (3,1) coordinate (B)
    --  (5,2) coordinate (C)
    --  (5,4) coordinate (D)
    --  (3,4) coordinate (E)
    --  (2.5,3) coordinate (F)
    --  (2,4) coordinate (G)
    --  (0,4) coordinate (H)
    --cycle ;

\draw[red,fill=red!25] (2.5,3.9) circle (.39) ;

\Cote{(A)}{(B)}{1}

\end{tikzpicture}
\end{document}  

lualatex 编译器提示未定义文档命令 Cote,因此无法绘制尺寸。有什么想法吗?

答案1

这很好用:

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usetikzlibrary{calc}

\usepackage{ifluatex}
\ifluatex
% LuaTeX does not provide all primitives of pdfTeX.
% Many of the missing are implemented by package pdftexcmds
% via Lua code.
% Thus the following example makes \pdfstrcmp and
% \pdffilemoddate available for LuaTeX:
\usepackage{pdftexcmds}
\makeatletter
\let\pdfstrcmp\pdf@strcmp
\let\pdffilemoddate\pdf@filemoddate
\makeatother
\fi

%% Dimension Code %%
\tikzset{%
    Cote node/.style={%
        midway,
        sloped,
        fill=white,
        inner sep=1.5pt,
        outer sep=2pt
    },
    Cote arrow/.style={%
        <->,
        >=latex,
        very thin
        }
    }

\makeatletter
\NewDocumentCommand{\Cote}{%
    s       % cotation avec les flèches à l'extérieur
    D<>{1.5pt} % offset des traits
    O{.75cm}    % offset de cotation
    m       % premier point
    m       % second point
    m       % étiquette
    D<>{o}  % () coordonnées -> angle
            % h -> horizontal,
            % v -> vertical
            % o or what ever -> oblique
    O{}     % parametre du tikzset
    }{%

{\tikzset{#8}

\coordinate (@1) at #4 ;
\coordinate (@2) at #5 ;

\if #7v % Cotation verticale
    \coordinate (@0) at ($($#4!.5!#5$) + (#3,0)$) ; 
    \coordinate (@4) at (@0|-@1) ;
    \coordinate (@5) at (@0|-@2) ;
\else
\if #7h % Cotation horizontale
    \coordinate (@0) at ($($#4!.5!#5$) + (0,#3)$) ; 
    \coordinate (@4) at (@0-|@1) ;
    \coordinate (@5) at (@0-|@2) ;
\else % cotation encoche
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
    \coordinate (@5) at ($#7!#3!#5$) ;
    \coordinate (@4) at ($#7!#3!#4$) ;
\else % cotation oblique    
    \coordinate (@5) at ($#5!#3!90:#4$) ;
    \coordinate (@4) at ($#4!#3!-90:#5$) ;
\fi\fi\fi

\draw[very thin,shorten >= #2,shorten <= -2*#2] (@4) -- #4 ;
\draw[very thin,shorten >= #2,shorten <= -2*#2] (@5) -- #5 ;

\IfBooleanTF #1 {% avec étoile
\draw[Cote arrow,-] (@4) -- (@5)
    node[Cote node] {#6\strut};
\draw[Cote arrow,<-] (@4) -- ($(@4)!-6pt!(@5)$) ;   
\draw[Cote arrow,<-] (@5) -- ($(@5)!-6pt!(@4)$) ;   
}{% sans étoile
\ifnum\pdfstrcmp{\unexpanded\expandafter{\@car#7\@nil}}{(}=\z@
    \draw[Cote arrow] (@5) to[bend right]
        node[Cote node] {#6\strut} (@4) ;
\else
\draw[Cote arrow] (@4) -- (@5)
    node[Cote node] {#6\strut};
\fi
}}
}
\makeatother

%% Document %%
\begin{document}
\begin{tikzpicture}
\small
\draw[thick,blue,fill=blue!25]
        (0,1) coordinate (A)
    --  (3,1) coordinate (B)
    --  (5,2) coordinate (C)
    --  (5,4) coordinate (D)
    --  (3,4) coordinate (E)
    --  (2.5,3) coordinate (F)
    --  (2,4) coordinate (G)
    --  (0,4) coordinate (H)
    --cycle ;

\draw[red,fill=red!25] (2.5,3.9) circle (.39) ;

\Cote{(A)}{(B)}{1}

\end{tikzpicture}
\end{document}  

相关内容