Le code ci-dessous compile sans erreur mais ne réalise pas ce à quoi je m'attendais.

Le résultat attendu est le suivant :

  • la macro « CrayonGris » dessine un crayon (avec ou sans bague, option définie par le booléen cmdDES@CrayonGris@bague du package xkeyval) ;
  • dans le cas où une bague est dessinée autour du crayon, un second booléen cmdDES@CrayonGris@orientation doit permettre de modifier la valeur de la variable \n à 1 (pour cmdDES@CrayonGris@orientation = true) ou -1 (pour cmdDES@CrayonGris@orientation = false) afin d'ajouter une vis à droite (pour \n = 1) ou bien à gauche (pour \n = -1).

Le code placé au niveau des deux « si - alors - sinon » semble ignoré. Tandis qu'en l'absence des tests la bague et la vis sont correctement dessinées.

Ouvrir dans l'éditeur Overleaf
\documentclass[12pt,a4paper]{article}
\usepackage[latin1,utf8]{inputenc}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{lmodern}
\usepackage{fourier}
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm]{geometry}
\usepackage{tikz}
    \usetikzlibrary{patterns}
\usepackage{tkz-euclide}
    \usetkzobj{all}
\usepackage{xkeyval}
\usepackage{ifthen}

\makeatletter
%----------> \CrayonGris <----------
\definecolor{wood}{cmyk}{0.00,0.12,0.35,0.05}
\define@cmdkey [DES] {CrayonGris} {inclinaison}{}    
\define@cmdkey [DES] {CrayonGris} {mine}{}
\define@cmdkey [DES] {CrayonGris} {corps}{}
\define@cmdkey [DES] {CrayonGris} {couleur}{}
\define@cmdkey [DES] {CrayonGris} {xscale}{}
\define@cmdkey [DES] {CrayonGris} {yscale}{}
\define@boolkey [DES] {CrayonGris} {bague}[true]{}
\define@boolkey [DES] {CrayonGris} {orientation}[true]{}
\presetkeys    [DES] {CrayonGris} {inclinaison = 0, mine = red, corps = blue, couleur = wood, xscale=1, yscale=1}{}
%
\newcommand{\CrayonGris}[2][]{%
    \setkeys[DES]{CrayonGris}{#1}
    \begin{scope}[shift={(#2)},rotate=\cmdDES@CrayonGris@inclinaison, xscale=\cmdDES@CrayonGris@xscale, yscale=\cmdDES@CrayonGris@yscale]
% Pointe ouverte
    \draw[fill=\cmdDES@CrayonGris@couleur] (-0.3,0.6) -- (0,0) -- (0.3,0.6) arc(-90:-180:0.1) arc(0:-180:0.1) arc(0:-180:0.1) arc(0:-90:0.1);
% Mine
    \draw[fill=\cmdDES@CrayonGris@mine] (-0.1,0.2) -- (0.1,0.2) -- (0,0) -- cycle;
% Corps du crayon    
    \draw[fill=\cmdDES@CrayonGris@corps] (0.3,0.6) arc(-90:-180:0.1) arc(0:-180:0.1) arc(0:-180:0.1) arc(0:-90:0.1) -- (-0.3,6) -- (0.3,6) -- cycle;
% Traits verticaux
    \draw (-0.2,6) -- (-0.2,0.65) (0,6) -- (0,0.65) (0.2,6) -- (0.2,0.65);
% Bague
    \if boolDES@CrayonGris@bague
    {
    \draw[fill=gray!20,opacity=0.5] (-0.4,1.5)rectangle(0.4,2.5);
    % Vis
    \if boolDES@CrayonGris@orientation
        {
        let \n = 1
        \draw (1,1) node{A};
        }
        \else
        {
        let \n = -1
        \draw(-1,-1) node{B}
        }
   \fi
\draw[fill=gray!20] (0.4*\n,1.9)--(0.6*\n,1.9)--(0.6*\n,1.8)--(0.8*\n,1.8)--(0.8*\n,2.2)--(0.6*\n,2.2)--(0.6*\n,2.1)--(0.4*\n,2.1)--cycle;
%\draw[fill=gray!20] (0.4,1.9)--(0.6,1.9)--(0.6,1.8)--(0.8,1.8)--(0.8,2.2)--(0.6,2.2)--(0.6,2.1)--(0.4,2.1)--cycle;
}
    \else
    \fi
%\ifthenelse{cmdDES@CrayonGris@orientation}{\n = 1}{\n = 2}
    \end{scope}
}
\makeatother

\begin{document}

\section*{Crayon gris avec bague et vis}

\begin{tikzpicture}
    \CrayonGris{0,0} 
    \CrayonGris[inclinaison=60,corps=gray,mine=black,couleur=white,xscale=1,yscale=1]{0,0}
    \CrayonGris[inclinaison=-80,corps=orange,mine=yellow,couleur=white,xscale=1.5,yscale=.75]{0,0}
\end{tikzpicture}

\begin{tikzpicture}
    \CrayonGris[bague=true,orientation=false,inclinaison=-90,corps=red,xscale=2,yscale=1.5]{0,0} 
    \CrayonGris[bague=true,orientation=true,corps=green]{0,0}
    \CrayonGris[bague=false,orientation=true,corps=white,inclinaison=90]{0,0} 
    \CrayonGris[bague=false,orientation=true,inclinaison=180]{0,0}
\end{tikzpicture}

\end{document}

Posée 11 Oct '19, 19:43

pdesmons's gravatar image

pdesmons
274817
Taux d'acceptation : 40%

Modifiée 12 Oct '19, 07:01

Pathe's gravatar image

Pathe ♦♦
7.4k29197245


Si j'ai bien compris la question, voici un code qui compile :

Ouvrir dans l'éditeur Overleaf
\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usepackage{xkeyval}

\newcount\n

\makeatletter
%----------> \CrayonGris <----------
\definecolor{wood}{cmyk}{0.00,0.12,0.35,0.05}
\define@cmdkey [DES] {CrayonGris} {inclinaison}{}    
\define@cmdkey [DES] {CrayonGris} {mine}{}
\define@cmdkey [DES] {CrayonGris} {corps}{}
\define@cmdkey [DES] {CrayonGris} {couleur}{}
\define@cmdkey [DES] {CrayonGris} {xscale}{}
\define@cmdkey [DES] {CrayonGris} {yscale}{}
\define@boolkey [DES] {CrayonGris} {bague}[true]{}
\define@boolkey [DES] {CrayonGris} {orientation}[true]{}
\presetkeys    [DES] {CrayonGris} {inclinaison = 0, mine = red, corps = blue, couleur = wood, xscale=1, yscale=1}{}
%
\newcommand{\CrayonGris}[2][]{%
    \setkeys[DES]{CrayonGris}{#1}
    \begin{scope}[shift={(#2)},rotate=\cmdDES@CrayonGris@inclinaison, xscale=\cmdDES@CrayonGris@xscale, yscale=\cmdDES@CrayonGris@yscale]
% Pointe ouverte
    \draw[fill=\cmdDES@CrayonGris@couleur] (-0.3,0.6) -- (0,0) -- (0.3,0.6) arc(-90:-180:0.1) arc(0:-180:0.1) arc(0:-180:0.1) arc(0:-90:0.1);
% Mine
    \draw[fill=\cmdDES@CrayonGris@mine] (-0.1,0.2) -- (0.1,0.2) -- (0,0) -- cycle;
% Corps du crayon    
    \draw[fill=\cmdDES@CrayonGris@corps] (0.3,0.6) arc(-90:-180:0.1) arc(0:-180:0.1) arc(0:-180:0.1) arc(0:-90:0.1) -- (-0.3,6) -- (0.3,6) -- cycle;
% Traits verticaux
    \draw (-0.2,6) -- (-0.2,0.65) (0,6) -- (0,0.65) (0.2,6) -- (0.2,0.65);
% Bague
    \ifDES@CrayonGris@bague
    \draw[fill=gray!20,opacity=0.5] (-0.4,1.5)rectangle(0.4,2.5);
    % Vis
    \ifDES@CrayonGris@orientation
        \n = 1
        \draw (1,1) node{A};
        \else
         \n = -1
        \draw(-1,-1) node{B};
   \fi
\draw[fill=gray!20] (0.4*\n,1.9)--(0.6*\n,1.9)--(0.6*\n,1.8)--(0.8*\n,1.8)--(0.8*\n,2.2)--(0.6*\n,2.2)--(0.6*\n,2.1)--(0.4*\n,2.1)--cycle;
%\draw[fill=gray!20] (0.4,1.9)--(0.6,1.9)--(0.6,1.8)--(0.8,1.8)--(0.8,2.2)--(0.6,2.2)--(0.6,2.1)--(0.4,2.1)--cycle;
    \fi
    \end{scope}
}
\makeatother

\begin{document}

\section*{Crayon gris avec bague et vis}

\begin{tikzpicture}
    \CrayonGris{0,0} 
    \CrayonGris[inclinaison=60,corps=gray,mine=black,couleur=white,xscale=1,yscale=1]{0,0}
    \CrayonGris[inclinaison=-80,corps=orange,mine=yellow,couleur=white,xscale=1.5,yscale=.75]{0,0}
\end{tikzpicture}

\begin{tikzpicture}
    \CrayonGris[bague=true,orientation=false,inclinaison=-90,corps=red,xscale=2,yscale=1.5]{0,0} 
    \CrayonGris[bague=true,orientation=true,corps=green]{0,0}
    \CrayonGris[bague=false,orientation=true,corps=white,inclinaison=90]{0,0} 
    \CrayonGris[bague=false,orientation=true,inclinaison=180]{0,0}
\end{tikzpicture}

\end{document}

Remarque : vous pouvez utiliser \def\n{1} et \def\n{-1} si vous voulez.

Lien permanent

Publiée 12 Oct '19, 00:47

touhami's gravatar image

touhami
9.7k410
Taux d'acceptation : 51%

Modifiée 12 Oct '19, 06:58

Pathe's gravatar image

Pathe ♦♦
7.4k29197245

C'est exactement ce qu'il me fallait !

(12 Oct '19, 09:39) pdesmons pdesmons's gravatar image
2

@pdesmons : dans ce cas, veuillez « accepter » la réponse en cliquant sur la boîte ad hoc, en haut à gauche de la réponse.

(12 Oct '19, 13:19) Pathe ♦♦ Pathe's gravatar image
Votre réponse
(dés)activer l'aperçu

Suivre cette question

Par courriel :

Une fois que vous serez enregistré, vous pourrez souscrire à n'importe quelle mise à jour ici

Par flux RSS :

Réponses

Réponses et commentaires

Bases de Markdown

  • *italique* ou _italique_
  • **gras** ou __gras__
  • Lien ::[texte](http://url.com/ "Titre ")
  • Image : ?![alt texte](/path/img.jpg "Titre ")
  • Liste numérotée : 1. Foo 2. Bar
  • Pour ajouter un passage à la ligne, ajoutez deux espaces à l'endroit où vous souhaitez que la ligne commence.
  • Les balises HTML de base sont également prises en charge.