L'objectif du code ci-dessous est de pouvoir dessiner un compas avec un crayon.

Après avoir déjà obtenu de l'aide sur texnique.fr, j'ai pu modifié l'orientation de la bague autour du crayon. Et je cherche maintenant à modifier l'orientation de mon compas sur le même principe.

Pour cela, j'ai adapté la solution qui m'a été proposée pour le crayon (et qui fonctionne exactement comme je le souhaite) mais je reçois un message d'erreur m'indiquant qu'il n'y a pas de valeur pour mon booléen DES@Compas@orientationCompas dont je souhaite affecter la valeur à l'autre booléen DES@CrayonGris@orientation.

Dans la documentation que je consulte (et que je ne comprends toujours pas), il est mentionné le recours aux actions \savevalue et \usevalue dans un \setkeys mais lorsque je les place dans mon code (au niveau du second test conditionnel qui affecte la valeur du compteur \c, cela ne fonctionne pas : le compas change bien d'orientation mais pas le crayon).

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}

\newcount\n
\newcount\c

\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@cmdkey [DES] {CrayonGris} {couleurBague}{}
\define@cmdkey [DES] {CrayonGris} {couleurVis}{}
\define@cmdkey [DES] {CrayonGris} {opacite}{}
\define@boolkey [DES] {CrayonGris} {orientation}[true]{}
\presetkeys    [DES] {CrayonGris} 
{
    inclinaison = 0, 
    mine = red, 
    corps = blue, 
    couleur = wood, 
    xscale=1, 
    yscale=1,
    couleurBague = gray!50,
    couleurVis = gray!50,
    opacite = 0.5
}{}
%
\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=\cmdDES@CrayonGris@couleurBague,opacity=\cmdDES@CrayonGris@opacite] (-0.4,1.5)rectangle(0.4,2.5);
    % Vis
    \ifDES@CrayonGris@orientation
        \n = 1
        \else
        \n = -1
   \fi
\draw[fill=\cmdDES@CrayonGris@couleurVis] (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

\makeatletter
%----------> \Compas <----------
\definecolor{wood}{cmyk}{0.00,0.12,0.35,0.05}
\define@cmdkey [DES] {Compas} {inclinaisonCompas}{}
\define@cmdkey [DES] {Compas} {ecartementCompas}{}
\define@cmdkey [DES] {Compas} {couleurCompasBrancheCrayon}{}
\define@cmdkey [DES] {Compas} {couleurCompasCrayonMine}{}
\define@cmdkey [DES] {Compas} {couleurCompasCrayonCorps}{}
\define@cmdkey [DES] {Compas} {couleurCompasCrayonCouleur}{}
\define@cmdkey [DES] {Compas} {couleurCompasBague}{}
\define@cmdkey [DES] {Compas} {couleurCompasVis}{}
\define@cmdkey [DES] {Compas} {couleurCompasBranchePointe}{}
\define@cmdkey [DES] {Compas} {couleurCompasPointe}{}
\define@cmdkey [DES] {Compas} {couleurCompasTop}{}
\define@cmdkey [DES] {Compas} {couleurCompasTopVisSerrage}{}
\define@cmdkey [DES] {Compas} {couleurCompasTopTrouSerrage}{}
\define@cmdkey [DES] {Compas} {xscale}{}
\define@cmdkey [DES] {Compas} {yscale}{}
\define@boolkey [DES] {Compas} {bagueCompas}[true]{}
\define@boolkey [DES] {Compas} {orientationCompas}[true]{}
\presetkeys    [DES] {Compas} 
{
    inclinaisonCompas = 0, 
    ecartementCompas = 0,
    couleurCompasBrancheCrayon = black,
    couleurCompasCrayonMine = black, 
    couleurCompasCrayonCorps = blue,
    couleurCompasCrayonCouleur = wood,
    couleurCompasBague = gray!50,
    couleurCompasVis = yellow,
    couleurCompasBranchePointe = black, 
    couleurCompasPointe = gray!50, 
    couleurCompasTop = yellow, 
    couleurCompasTopVisSerrage = gray!30,
    couleurCompasTopTrouSerrage = gray!20,
    xscale=1, 
    yscale=1
}{}
%
\newcommand{\Compas}[2][]{%
    \setkeys[DES]{Compas}{#1}
    \begin{scope}[shift={(#2)},rotate=\cmdDES@Compas@inclinaisonCompas, xscale=\cmdDES@Compas@xscale, yscale=\cmdDES@Compas@yscale]
\ifDES@Compas@orientationCompas
    \c = 1  
    \setkeys[DES]{Compas}{\savevalue{orientationCompas}=true}
    \else
    \c = -1
    \setkeys[DES]{Compas}{\savevalue{orientationCompas}=false}
   \fi
\setkeys[DES]{CrayonGris}{\savevalue{orientation}=\usevalue{DES@Compas@orientationCompas}}
% Branche du compas avec la pointe
    \begin{scope}[xshift=-(\cmdDES@Compas@ecartementCompas cm)/2,
                  rotate={-asin(\cmdDES@Compas@ecartementCompas/20)}]
    % Pointe du compas
\draw[fill=\cmdDES@Compas@couleurCompasPointe] (0*\c,0)--(0*\c,0.4)--(-0.1*\c,0.2)--(-0.1*\c,0.1)--cycle;%gray!50
    % Branche pointe
\draw[fill=\cmdDES@Compas@couleurCompasBranchePointe,smooth,rounded corners=3pt] (0*\c,0.2)--(0*\c,5)--(-0.1*\c,7.5)--(0*\c,10)--(-0.6*\c,9.8)--(-0.8*\c,9)--(-0.85*\c,8)--(-0.9*\c,7)--(-0.9*\c,6)--(-0.8*\c,5)-- (-0.75*\c,4)--(-0.65*\c,3)--(-0.7*\c,2)--(-0.6*\c,1)--(-0.5*\c,0.7)--(-0.3*\c,0.25)--cycle;
    \end{scope}
% Branche du compas avec le crayon/la mine
    \begin{scope}[xshift=(\cmdDES@Compas@ecartementCompas cm)/2,rotate={asin(\cmdDES@Compas@ecartementCompas/20)}]
    % Branche crayon
\draw[fill=\cmdDES@Compas@couleurCompasBrancheCrayon,smooth,rounded corners=3pt] (0*\c,2)--(0*\c,5)--(0.1*\c,7.5)--(0*\c,10)--(0.6*\c,9.8)--(0.8*\c,9)--(0.85*\c,8)--(0.9*\c,7)--(0.9*\c,6)--(0.8*\c,5)-- (0.75*\c,4)--(0.65*\c,3)--(0.7*\c,2)--(0*\c,2)--cycle;
    % Crayon fixé
\begin{scope}[rotate=-30] 
\CrayonGris[bague=true,
            orientation=true,
            corps=\cmdDES@Compas@couleurCompasCrayonCorps,
            mine=\cmdDES@Compas@couleurCompasCrayonMine,
            couleur=\cmdDES@Compas@couleurCompasCrayonCouleur,
            couleurBague=\cmdDES@Compas@couleurCompasBague,
            couleurVis=\cmdDES@Compas@couleurCompasVis,
            opacite=1,
            %orientation=\cmdDES@Compas@orientationCompas,
            couleurVis=\cmdDES@Compas@couleurCompasTop]{0,0}
    % Fixation
\draw[fill=\cmdDES@Compas@couleurCompasBague](-0.4,2.5)--(-1.355,2.35)--(-0.86,1.5)--(-0.4,1.5)--cycle;
\end{scope}
    \end{scope}
% Top du compas
    \begin{scope}[yshift=-10cm+0.5cm*sqrt(400-\cmdDES@Compas@ecartementCompas*\cmdDES@Compas@ecartementCompas)]
    % Top
\draw[fill=\cmdDES@Compas@couleurCompasTop,rounded corners=4pt] (0,8.6)--(0.3,9)--(0.6,9.8)--(0.7,10)--(0.5,11)--(0.3,12) arc(0:180:0.3)--(-0.5,11)--(-0.7,10)--(-0.6,9.8)--(-0.3,9)--cycle;
    % Trou
\draw[fill=\cmdDES@Compas@couleurCompasTopTrouSerrage] (0,10) circle (0.3);
    % Vis de serrage
\draw[fill=\cmdDES@Compas@couleurCompasTopVisSerrage] (0.125,9.785) -- ++(60:0.25) -- ++(120:0.25) -- ++(180:0.25) -- ++(240:0.25) -- ++(300:0.25) -- cycle;
\draw[red](0,10)node{$\times$}; %vérification visuelle du positionnement de la pièce Top
    \end{scope}
    \end{scope}
}
\makeatother

\begin{document}

\section*{Compas}

\subsection*{Crayon (avec ou sans bague/vis)}

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

\subsection*{Compas}

\begin{tikzpicture}
    \Compas{0,0} 
\end{tikzpicture}

\begin{tikzpicture}
    \Compas[inclinaisonCompas=-30,
            couleurCompasTop=orange,
            couleurCompasTopVisSerrage=red,
            couleurCompasTopTrouSerrage=blue,
            couleurCompasBranchePointe=yellow,
            couleurCompasPointe=red,
            couleurCompasBrancheCrayon=green,
            couleurCompasBague=black,
            couleurCompasVis=yellow,
            orientationCompas=false]{0,0}
\end{tikzpicture}

\begin{tikzpicture}
    \Compas[couleurCompasTop=white,
            couleurCompasTopVisSerrage=white,
            couleurCompasTopTrouSerrage=white,
            couleurCompasBranchePointe=white,
            couleurCompasBrancheCrayon=white,
            couleurCompasCrayonCouleur=white,
            couleurCompasCrayonCorps=white,
            couleurCompasVis=white,
            couleurCompasBague=white]{0,0}
\end{tikzpicture}

\subsection*{Exemples}

\begin{tikzpicture}
    \clip(-7,-2)rectangle(7,13);
    \Compas[ecartementCompas=12,
            couleurCompasTop=orange,
            couleurCompasTopVisSerrage=blue,
            couleurCompasTopTrouSerrage=gray,
            couleurCompasBranchePointe=black,
            couleurCompasBrancheCrayon=green,
            couleurCompasBague=white]{0,0}
    \draw[red](0,0)node{$\times$}; 
    \draw[red](0,0)node[below]{O};
    \draw[red](0,8)node{$\times$}; 
    \draw[red](0,8)node[below]{C};
    \draw[red](-6,0)node{$\times$}; 
    \draw[red](-6,0)node[below]{A};
    \draw[red](6,0)node{$\times$}; 
    \draw[red](6,0)node[below]{B};
    \draw[red](-6,10)node{$\times$}; 
    \draw[red](-6,10)node[below]{D};
    \draw[red](-6,0)circle(10);
    \draw[red](6,10)node{$\times$}; 
    \draw[red](6,10)node[below]{E};
    \draw[red](6,0)circle(10);
\end{tikzpicture}

\begin{tikzpicture}
    \clip(-7,-2)rectangle(7,13);
    \Compas[ecartementCompas=6]{0,0}
    \draw[red](0,0)node{$\times$}; 
    \draw[red](0,0)node[below]{O};
    \draw[red](0,8)node{$\times$}; 
    \draw[red](0,8)node[below]{C};
    \draw[red](-3,0)node{$\times$}; 
    \draw[red](-3,0)node[below]{A};
    \draw[red](3,0)node{$\times$}; 
    \draw[red](3,0)node[below]{B};
    \draw[red](-3,10)node{$\times$}; 
    \draw[red](-3,10)node[below]{D};
    \draw[red](-3,0)circle(10);
    \draw[red](3,10)node{$\times$}; 
    \draw[red](3,10)node[below]{E};
    \draw[red](3,0)circle(10);
\end{tikzpicture}

\begin{tikzpicture}
    \clip(-7,-2)rectangle(7,13);
    \Compas[ecartementCompas=0.2,
            couleurCompasTop=white,
            couleurCompasTopVisSerrage=white,
            couleurCompasTopTrouSerrage=white,
            couleurCompasBranchePointe=white,
            couleurCompasBrancheCrayon=white,
            couleurCompasCrayonCouleur=white,
            couleurCompasCrayonCorps=white,
            couleurCompasVis=white,
            couleurCompasBague=white,
            orienationCompas=false]{0,0}
    \draw[red](0,0)node{$\times$}; 
    \draw[red](0,0)node[below]{O};
    \draw[red](0,8)node{$\times$}; 
    \draw[red](0,8)node[below]{C};
    \draw[red](-0.2,0)node{$\times$}; 
    \draw[red](-0.2,0)node[below]{A};
    \draw[red](0.2,0)node{$\times$}; 
    \draw[red](0.2,0)node[below]{B};
    \draw[red](-0.2,10)node{$\times$}; 
    \draw[red](-0.2,10)node[below]{D};
    \draw[red](-0.2,0)circle(10);
    \draw[red](0.2,10)node{$\times$}; 
    \draw[red](0.2,10)node[below]{E};
    \draw[red](0.2,0)circle(10);
\end{tikzpicture}

\end{document}

Posée 13 Oct '19, 17:28

pdesmons's gravatar image

pdesmons
274817
Taux d'acceptation : 40%

Modifiée 15 Oct '19, 16:23

Pathe's gravatar image

Pathe ♦♦
7.4k29196245

1

Sur overleaf, je ne vois pas le problème

(14 Oct '19, 12:55) joseph-tux joseph-tux's gravatar image

Et pourtant le code ne compile pas sur mon PC.

(14 Oct '19, 18:22) pdesmons pdesmons's gravatar image
1

@pdesmons : peut-être devriez-vous analyser le fichier .log, voire en ajouter des extraits à votre question.

(16 Oct '19, 10:43) Pathe ♦♦ Pathe's gravatar image

En testant le booléen "DES@Compas@orientationCompas" à true ou à false, j'obtiens un affichage incohérent de la macro : par exemple, les modifications des couleurs en option ne sont pas respectées...

(18 Oct '19, 19:47) pdesmons pdesmons's gravatar image
1

Que dit le fichier .log ?

(18 Oct '19, 19:51) Pathe ♦♦ Pathe's gravatar image

Le message d'erreur du fichier log indique : !Package xkeyval Error : no value recorded for key "DES@Compas@orientationCompas";ignored. See the xkeyval documentation for explanation. !Package xkeyval Error : value 'is not allowed'. See the ...

(18 Oct '19, 19:51) pdesmons pdesmons's gravatar image

LOG FILE : This is pdfTeX, Version 3.14159265-2.6-1.40.19 (TeX Live 2018/W32TeX) (preloaded format=pdflatex 2019.2.6) 18 OCT 2019 20:23 entering extended mode restricted write18 enabled. %&-line parsing enabled. **Compas.tex (./Compas.tex LaTeX2e <2018-12-01> (c:/texlive/2018/texmf-dist/tex/latex/base/article.cls Document Class: article 2018/09/03 v1.4i Standard LaTeX document class (c:/texlive/2018/texmf-dist/tex/latex/base/size12.clo File: size12.clo 2018/09/03 v1.4i Standard LaTeX file (size option) ) ...

(18 Oct '19, 21:30) pdesmons pdesmons's gravatar image

... LaTeX Font Info: Font shape T1/futs/bx/n' in size <17.28> not available (Font) Font shapeT1/futs/b/n' tried instead on input line 170. LaTeX Font Info: Font shape T1/futs/bx/n' in size <14.4> not available (Font) Font shapeT1/futs/b/n' tried instead on input line 172. Overfull hbox (18.02458pt too wide) in paragraph at lines 187--188 [][] [] ! Package xkeyval Error: no value recorded for key `DES@Compas@orientationCompa s'; ignored. See the xkeyval package documentation for explanation. Type H for immediate help. ...

(18 Oct '19, 21:31) pdesmons pdesmons's gravatar image

... l.205 orientationCompas=true]{0,0} Try typing to proceed. If that doesn't work, type X to quit. ! Package xkeyval Error: value `' is not allowed. See the xkeyval package documentation for explanation. Type H for immediate help. ... ... (c:/texlive/2018/texmf-dist/tex/latex/fourier/fmlfutmi.fd File: fmlfutmi.fd 2004/10/30 Fontinst v1.926 font definitions for FML/futmi. )

(18 Oct '19, 21:31) pdesmons pdesmons's gravatar image

LaTeX Font Info: Font shape FMX/futm/m/n' will be (Font) scaled to size 11.03998pt on input line 233. LaTeX Font Info: Font shapeFMX/futm/m/n' will be (Font) scaled to size 8.27998pt on input line 233. LaTeX Font Info: Font shape `FMX/futm/m/n' will be (Font) scaled to size 6.43999pt on input line 233. LaTeX Font Info: Try loading font information for U+msa on input line 233. (c:/texlive/2018/texmf-dist/tex/latex/amsfonts/umsa.fd File: umsa.fd 2013/01/14 v3.01 AMS symbols A )

(18 Oct '19, 21:32) pdesmons pdesmons's gravatar image

LaTeX Font Info: Try loading font information for U+msb on input line 233. (c:/texlive/2018/texmf-dist/tex/latex/amsfonts/umsb.fd File: umsb.fd 2013/01/14 v3.01 AMS symbols B ) LaTeX Font Info: Font shape U/futm/m/n' will be (Font) scaled to size 11.03998pt on input line 233. LaTeX Font Info: Font shapeU/futm/m/n' will be (Font) scaled to size 8.27998pt on input line 233. LaTeX Font Info: Font shape `U/futm/m/n' will be (Font) scaled to size 6.43999pt on input line 233.

(18 Oct '19, 21:33) pdesmons pdesmons's gravatar image

! Package xkeyval Error: no value recorded for key DES@Compas@orientationCompa s'; ignored. See the xkeyval package documentation for explanation. Type H <return> for immediate help. ... l.252 orientationCompas=true]{0,0} Try typing <return> to proceed. If that doesn't work, type X <return> to quit. ! Package xkeyval Error: value' is not allowed. See the xkeyval package documentation for explanation. Type H for immediate help. ... l.252 orientationCompas=true]{0,0} Try typing to proceed. If that doesn't work, type X to quit.

(18 Oct '19, 21:33) pdesmons pdesmons's gravatar image

[3] ! Package xkeyval Error: no value recorded for key DES@Compas@orientationCompa s'; ignored. See the xkeyval package documentation for explanation. Type H <return> for immediate help. ... l.280 couleurCompasBague=white]{0,0} %, Try typing <return> to proceed. If that doesn't work, type X <return> to quit. ! Package xkeyval Error: value' is not allowed. See the xkeyval package documentation for explanation. Type H for immediate help. ...

(18 Oct '19, 21:34) pdesmons pdesmons's gravatar image

l.280 couleurCompasBague=white]{0,0} %, Try typing to proceed. If that doesn't work, type X to quit. [4] [5] [6] (./Compas.aux) ) Here is how much of TeX's memory you used: 19917 strings out of 492616 387766 string characters out of 6132769 489115 words of memory out of 5000000 23315 multiletter control sequences out of 15000+600000 45876 words of font info for 52 fonts, out of 8000000 for 9000 1141 hyphenation exceptions out of 8191 55i,6n,64p,416b,1046s stack positions out of 5000i,500n,10000p,200000b,80000s

(18 Oct '19, 21:34) pdesmons pdesmons's gravatar image

{c:/texlive/2018/texmf-dist/fonts/enc/dvips/base/8r .enc} Output written on Compas.pdf (6 pages, 43011 bytes). PDF statistics: 39 PDF objects out of 1000 (max. 8388607) 27 compressed objects within 1 object stream 0 named destinations out of 1000 (max. 500000) 13 words of extra memory for PDF output out of 10000 (max. 10000000)

(18 Oct '19, 21:34) pdesmons pdesmons's gravatar image
1

Votre question n'est pas claire. Quel est votre objectif? est ce que c'est de faire le booléen orientation dépend de orientationCompas? c'est-à-dire quelque chose comme

\ifDES@Compas@orientationCompas \c = 1 \setkeys[DES]{CrayonGris}{orientation=true} \else \c = -1 \setkeys[DES]{CrayonGris}{orientation=false} \fi? Merci de bien préciser ce que vous voulez faire?

(19 Oct '19, 00:38) touhami touhami's gravatar image

La macro Crayongris fonctionne parfaitement et est indépendante de la macro Compas. Cette dernière utilise la première.

Le booléen «orientation» change la position (gauche ou droite) de la vis de la bague du crayon. Et le booléen «orientationCompas» permet de choisir si la pointe du compas est à droite ou à gauche avec, dans les deux cas, le crayon dessiné sur l'autre branche et donc la vis de la bague doit être orientée en fonction de la position de la pointe du compas.

Ainsi il faut contrôler la valeur de «orientation» en fonction de celle de «orientationCompas» dans la macro Compas.

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

Avec la modification proposée par touhami mon code compile sans erreur (!) et semble correctement fonctionner mais le résultat graphique n'est pas celui auquel je m'attendais donc il faut que je regarde de plus près le code.

(19 Oct '19, 13:46) pdesmons pdesmons's gravatar image

J'ai terminé mon code et j'obtiens grâce à toutes les aides qui m'ont été apportées le résultat final souhaité.

(19 Oct '19, 18:59) pdesmons pdesmons's gravatar image

@pdesmons : si nous nous réjouissons de la résolution de votre problème, nous serions heureux d'en savoir un peu plus. Pourriez-vous poster votre code final en y ajoutant des commentaires explicatifs ? Cette réponse serait utile à la communauté.

(20 Oct '19, 17:07) Pathe ♦♦ Pathe's gravatar image
Affichage de 5 parmi 20 Afficher 15 commentaire(s) en plus

Voici le code final (enfin presque : je souhaite encore modifier l'écartement par rapport à l'échelle xscale, mais après les vacances...) de ma macro « Compas » dont l'objectif est de dessiner un compas en choisissant :

  • la position et l'orientation droite-gauche de la pointe,
  • l'inclinaison du crayon par rapport à la pointe,
  • la couleur des divers éléments qui constitue le compas,
  • l'écartement évidemment...

Le code compile sans erreur grâce aux nombreux conseils donnés en commentaires à ma question, j'obtiens exactement ce que je souhaitais.

L'utilisation du l'excellent package xkeyval m'a beaucoup torturé, d'où mes questions sur ce forum d'experts !

Mes prochaines étapes seront l'équerre, la règle graduée et le rapporteur.

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}

\newcount\n
\newcount\c

\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@cmdkey [DES] {CrayonGris} {couleurBague}{}
\define@cmdkey [DES] {CrayonGris} {couleurVis}{}
\define@cmdkey [DES] {CrayonGris} {opacite}{}
\define@boolkey [DES] {CrayonGris} {orientation}[true]{}
\presetkeys    [DES] {CrayonGris} 
{
    inclinaison = 0, 
    mine = red, 
    corps = blue, 
    couleur = wood, 
    xscale=1, 
    yscale=1,
    couleurBague = gray!50,
    couleurVis = gray!50,
    opacite = 0.5
}{}
%
\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=\cmdDES@CrayonGris@couleurBague,opacity=\cmdDES@CrayonGris@opacite] (-0.4,1.5)rectangle(0.4,2.5);
    % Vis
    \ifDES@CrayonGris@orientation % choix de la position de la vis sur la bague (droite=1 ; gauche=-1)
        \n = 1
        \else
        \n = -1
   \fi
\draw[fill=\cmdDES@CrayonGris@couleurVis] (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

\makeatletter
%----------> \Compas <----------
\definecolor{wood}{cmyk}{0.00,0.12,0.35,0.05}
\define@cmdkey [DES] {Compas} {inclinaisonCompas}{}
\define@cmdkey [DES] {Compas} {ecartementCompas}{}
\define@cmdkey [DES] {Compas} {couleurCompasBrancheCrayon}{}
\define@cmdkey [DES] {Compas} {couleurCompasCrayonMine}{}
\define@cmdkey [DES] {Compas} {couleurCompasCrayonCorps}{}
\define@cmdkey [DES] {Compas} {couleurCompasCrayonCouleur}{}
\define@cmdkey [DES] {Compas} {couleurCompasBague}{}
\define@cmdkey [DES] {Compas} {couleurCompasVis}{}
\define@cmdkey [DES] {Compas} {couleurCompasBranchePointe}{}
\define@cmdkey [DES] {Compas} {couleurCompasPointe}{}
\define@cmdkey [DES] {Compas} {couleurCompasTop}{}
\define@cmdkey [DES] {Compas} {couleurCompasTopVisSerrage}{}
\define@cmdkey [DES] {Compas} {couleurCompasTopTrouSerrage}{}
\define@cmdkey [DES] {Compas} {xscale}{}
\define@cmdkey [DES] {Compas} {yscale}{}
\define@boolkey [DES] {Compas} {orientationCompas}[true]{}
\define@boolkey [DES] {Compas} {bagueCompas}[true]{}
\presetkeys    [DES] {Compas} 
{
    inclinaisonCompas = 0, 
    ecartementCompas = 0,
    couleurCompasBrancheCrayon = black,
    couleurCompasCrayonMine = black, 
    couleurCompasCrayonCorps = blue,
    couleurCompasCrayonCouleur = wood,
    couleurCompasBague = gray!50,
    couleurCompasVis = yellow,
    couleurCompasBranchePointe = black, 
    couleurCompasPointe = gray!50, 
    couleurCompasTop = yellow, 
    couleurCompasTopVisSerrage = gray!30,
    couleurCompasTopTrouSerrage = gray!20,
    xscale=1, 
    yscale=1
}{}
%
\newcommand{\Compas}[2][]{%
    \setkeys[DES]{Compas}{#1}
    \begin{scope}[shift={(#2)},rotate=\cmdDES@Compas@inclinaisonCompas, xscale=\cmdDES@Compas@xscale, yscale=\cmdDES@Compas@yscale]
\ifDES@Compas@orientationCompas % corrélation entre l'orientation du compas et de la vis sur la bague du compas 
    \c = 1  
    \setkeys[DES]{CrayonGris}{orientation=true}
    \else
    \c = -1
    \setkeys[DES]{CrayonGris}{orientation=false}
  \fi
% Branche du compas avec la pointe
    \begin{scope}[xshift=-(\cmdDES@Compas@ecartementCompas cm)*\c/2,
                  rotate={-asin(\cmdDES@Compas@ecartementCompas/20)*\c}]
    % Pointe du compas
\draw[fill=\cmdDES@Compas@couleurCompasPointe] (0*\c,0)--(0*\c,0.4)--(-0.1*\c,0.2)--(-0.1*\c,0.1)--cycle;
    % Branche pointe
\draw[fill=\cmdDES@Compas@couleurCompasBranchePointe,smooth,rounded corners=3pt] (0*\c,0.2)--(0*\c,5)--(-0.1*\c,7.5)--(0*\c,10)--(-0.6*\c,9.8)--(-0.8*\c,9)--(-0.85*\c,8)--(-0.9*\c,7)--(-0.9*\c,6)--(-0.8*\c,5)-- (-0.75*\c,4)--(-0.65*\c,3)--(-0.7*\c,2)--(-0.6*\c,1)--(-0.5*\c,0.7)--(-0.3*\c,0.25)--cycle;
    \end{scope}
% Branche du compas avec le crayon/la mine
    \begin{scope}[xshift=(\cmdDES@Compas@ecartementCompas cm)*\c/2,rotate={asin(\cmdDES@Compas@ecartementCompas/20)*\c}]
    % Branche crayon
\draw[fill=\cmdDES@Compas@couleurCompasBrancheCrayon,smooth,rounded corners=3pt] (0*\c,2)--(0*\c,5)--(0.1*\c,7.5)--(0*\c,10)--(0.6*\c,9.8)--(0.8*\c,9)--(0.85*\c,8)--(0.9*\c,7)--(0.9*\c,6)--(0.8*\c,5)-- (0.75*\c,4)--(0.65*\c,3)--(0.7*\c,2)--(0*\c,2)--cycle;
    % Crayon fixé
\begin{scope}[rotate=-30*\c] 
\CrayonGris[bague=true,
            corps=\cmdDES@Compas@couleurCompasCrayonCorps,
            mine=\cmdDES@Compas@couleurCompasCrayonMine,
            couleur=\cmdDES@Compas@couleurCompasCrayonCouleur,
            couleurBague=\cmdDES@Compas@couleurCompasBague,
            couleurVis=\cmdDES@Compas@couleurCompasVis,
            opacite=1,
            couleurVis=\cmdDES@Compas@couleurCompasTop]{0,0}
    % Fixation
\draw[fill=\cmdDES@Compas@couleurCompasBague](-0.4*\c,2.5)--(-1.355*\c,2.35)--(-0.86*\c,1.5)--(-0.4*\c,1.5)--cycle;
\end{scope}
    \end{scope}
% Top du compas
    \begin{scope}[yshift=-10cm+0.5cm*sqrt(400-\cmdDES@Compas@ecartementCompas*\cmdDES@Compas@ecartementCompas)]
    % Top
\draw[fill=\cmdDES@Compas@couleurCompasTop,rounded corners=4pt] (0,8.6)--(0.3,9)--(0.6,9.8)--(0.7,10)--(0.5,11)--(0.3,12) arc(0:180:0.3)--(-0.5,11)--(-0.7,10)--(-0.6,9.8)--(-0.3,9)--cycle;
    % Trou
\draw[fill=\cmdDES@Compas@couleurCompasTopTrouSerrage] (0,10) circle (0.3);
    % Vis de serrage
\draw[fill=\cmdDES@Compas@couleurCompasTopVisSerrage] (0.125,9.785) -- ++(60:0.25) -- ++(120:0.25) -- ++(180:0.25) -- ++(240:0.25) -- ++(300:0.25) -- cycle;
%\draw[red](0,10)node{$\times$}; %vérification visuelle du positionnement de la pièce Top
    \end{scope}
    \end{scope}
}
\makeatother

\begin{document}

\title{Compas}
\maketitle

\section{Crayon (avec ou sans bague/vis)}

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

\section{Compas}

\subsection{Compas avec crayon à droite}

\begin{tikzpicture}
    \Compas[orientationCompas=true]{0,0} 
\end{tikzpicture}

\subsection{Compas avec crayon à gauche}

\begin{tikzpicture}
    \Compas[orientationCompas=false]{0,0} 
\end{tikzpicture}

\subsection{Changement de couleurs et d'inclinaison}

\begin{tikzpicture}
    \Compas[inclinaisonCompas=-30,
            couleurCompasTop=orange,
            couleurCompasTopVisSerrage=red,
            couleurCompasTopTrouSerrage=blue,
            couleurCompasBranchePointe=yellow,
            couleurCompasPointe=red,
            couleurCompasBrancheCrayon=green,
            couleurCompasBague=black,
            couleurCompasVis=yellow,
            orientationCompas=true]{0,0}
\end{tikzpicture}

\begin{tikzpicture}
    \Compas[couleurCompasTop=white,
            couleurCompasTopVisSerrage=white,
            couleurCompasTopTrouSerrage=white,
            couleurCompasBranchePointe=white,
            couleurCompasBrancheCrayon=white,
            couleurCompasCrayonCouleur=white,
            couleurCompasCrayonCorps=white,
            couleurCompasVis=white,
            couleurCompasBague=white,
            orientationCompas=true]{0,0}
\end{tikzpicture}

\subsection{Exemples de changement d'écartement}

\paragraph{Ecartement de 12 cm}

\begin{tikzpicture}
    \clip(-7,-2)rectangle(7,13);
    \Compas[ecartementCompas=12,
            couleurCompasTop=orange,
            couleurCompasTopVisSerrage=blue,
            couleurCompasTopTrouSerrage=gray,
            couleurCompasBranchePointe=black,
            couleurCompasBrancheCrayon=green,
            couleurCompasBague=white,
            orientationCompas=true]{0,0}
    % vérification de la position des divers éléments : branches et top par rapport à l'écartement
    \draw[red](0,0)node{$\times$}; 
    \draw[red](0,0)node[below]{O};
    \draw[red](0,8)node{$\times$}; 
    \draw[red](0,8)node[below]{C};
    \draw[red](-6,0)node{$\times$}; 
    \draw[red](-6,0)node[below]{A};
    \draw[red](6,0)node{$\times$}; 
    \draw[red](6,0)node[below]{B};
    \draw[red](-6,10)node{$\times$}; 
    \draw[red](-6,10)node[below]{D};
    \draw[red](-6,0)circle(10);
    \draw[red](6,10)node{$\times$}; 
    \draw[red](6,10)node[below]{E};
    \draw[red](6,0)circle(10);
\end{tikzpicture}

\paragraph{Ecartement de 6 cm}

\begin{tikzpicture}
    \clip(-7,-2)rectangle(7,13);
    \Compas[ecartementCompas=6,
            orientationCompas=false]{0,0}
    \draw[red](0,0)node{$\times$}; 
    \draw[red](0,0)node[below]{O};
    \draw[red](0,8)node{$\times$}; 
    \draw[red](0,8)node[below]{C};
    \draw[red](-3,0)node{$\times$}; 
    \draw[red](-3,0)node[below]{A};
    \draw[red](3,0)node{$\times$}; 
    \draw[red](3,0)node[below]{B};
    \draw[red](-3,10)node{$\times$}; 
    \draw[red](-3,10)node[below]{D};
    \draw[red](-3,0)circle(10);
    \draw[red](3,10)node{$\times$}; 
    \draw[red](3,10)node[below]{E};
    \draw[red](3,0)circle(10);
\end{tikzpicture}

\paragraph{Ecartement de 0,2 cm}

\begin{tikzpicture}
    \clip(-7,-2)rectangle(7,13);
    \Compas[ecartementCompas=0.2,
            couleurCompasTop=white,
            couleurCompasTopVisSerrage=white,
            couleurCompasTopTrouSerrage=white,
            couleurCompasBranchePointe=white,
            couleurCompasBrancheCrayon=white,
            couleurCompasCrayonCouleur=white,
            couleurCompasCrayonCorps=white,
            couleurCompasVis=white,
            couleurCompasBague=white,
            orientationCompas=true]{0,0}
    \draw[red](0,0)node{$\times$}; 
    \draw[red](0,0)node[below]{O};
    \draw[red](0,8)node{$\times$}; 
    \draw[red](0,8)node[below]{C};
    \draw[red](-0.2,0)node{$\times$}; 
    \draw[red](-0.2,0)node[below]{A};
    \draw[red](0.2,0)node{$\times$}; 
    \draw[red](0.2,0)node[below]{B};
    \draw[red](-0.2,10)node{$\times$}; 
    \draw[red](-0.2,10)node[below]{D};
    \draw[red](-0.2,0)circle(10);
    \draw[red](0.2,10)node{$\times$}; 
    \draw[red](0.2,10)node[below]{E};
    \draw[red](0.2,0)circle(10);
\end{tikzpicture}

\paragraph{Agrandissement et réduction}

\begin{tikzpicture}
    \Compas[couleurCompasTop=orange,
            couleurCompasTopVisSerrage=red,
            couleurCompasTopTrouSerrage=blue,
            couleurCompasBranchePointe=yellow,
            couleurCompasPointe=red,
            couleurCompasBrancheCrayon=green,
            couleurCompasBague=black,
            couleurCompasVis=yellow,
            xscale=1.5]{0,0}
\end{tikzpicture}

\begin{tikzpicture}
    \Compas[couleurCompasTop=orange,
            couleurCompasTopVisSerrage=red,
            couleurCompasTopTrouSerrage=blue,
            couleurCompasBranchePointe=yellow,
            couleurCompasPointe=red,
            couleurCompasBrancheCrayon=green,
            couleurCompasBague=black,
            couleurCompasVis=yellow,
            yscale=0.5]{0,0}
\end{tikzpicture}

\end{document}
Lien permanent

Publiée 20 Oct '19, 18:01

pdesmons's gravatar image

pdesmons
274817
Taux d'acceptation : 40%

Modifiée 20 Oct '19, 18:05

Pathe's gravatar image

Pathe ♦♦
7.4k29196245

@pdesmons : formidable ! Merci beaucoup. Pourriez-vous également fournir une réponse à votre question sur les interrupteurs booléens ? Je crains que sinon, elle ne reste en déshérence. Par ailleurs, le problème exposé dans cette première question étant moins vaste que celui exposé ci-dessus, la réponse que vous écririez serait une utile introduction aux booléens tels qu'utilisés par le package xkeyval.

(20 Oct '19, 18:10) Pathe ♦♦ Pathe's gravatar image
3

Bravo ! Ça pourrait tout à fait être l'objet d'un package que vous pourriez distribuer sur le CTAN !

(26 Oct '19, 10:34) denis ♦♦ denis's gravatar image
1

C'est effectivement mon objectif ultime si le résultat avec l'ensemble des instruments de géométrie me convient suffisamment.

(27 Oct '19, 16:52) pdesmons pdesmons'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.