Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am a beginner in LaTex, trying to use TikZ to draw a series of RF cables for industrial documents. On the MWE below, I cannot find a way to prevent overlapping the threads of the braid while preserving the alignment of the threaded strands. Also I wonder if the code can be improved, as I have lots of different wires to handle, some with dual braids, etc....

\documentclass{article} 
\usepackage{tikz}
\usetikzlibrary{positioning, calc, arrows,backgrounds}
\begin{document}
\begin{tikzpicture}[ >=latex',auto,inner sep=2mm,node distance=4cm and 6cm]
\tikzset{box1/.style={draw,minimum width=2cm,rectangle, rounded corners =1 pt,  line width=2pt}}

\node[box1, text width = 2 cm,text height= 1 cm] (jacket) {};
\node[box1,  text width = 2cm,text height= 0.8 cm, right=- 2pt of jacket] (braid)  {};
\node[box1, text width = 1.5cm,text height= 0.5 cm, right=-2pt of braid] (insul)  {};

\node[anchor=center] (A) at (0, -0.9) {};
\node[anchor=center] (B) at (0.2,  0.9) {};
\draw[anchor=center] (A) .. controls (-0.2, -0.2) and (0.2, 0.2) .. (B);
\node[anchor=center] (C) at (0.1, -0.9) {};
\node[anchor=center] (D) at (0.3,  0.9) {};
\draw[anchor=center] (C) .. controls (-0.1, -0.2) and (0.3, 0.2) .. (D);

\clip (1.2, -0.6) rectangle (3.6,0.6); %clipping braided area

\foreach \x in {1,2,...,8} %starting drawing patterns for braid
 \draw [anchor=center, xshift=1.5cm, yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8}
\draw  [anchor=center,  xshift=2.1cm,  yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=2.7cm,  yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=3.3cm, yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=3.9cm,  yshift =0.8 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);

\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 0.9cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 1.5cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.1cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.7cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 3.3cm, yshift=1.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);

\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=1.6cm, yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8}
\draw  [anchor=center,  xshift=2.2cm,  yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=2.8cm,  yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=3.4cm, yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=4cm,  yshift =-6 mm, rotate=45] (\x/19,0) -- (\x/19,0.6);

\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift=0.9 cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 1.5cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.1cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 2.7cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);
\foreach \x in {1,2,...,8} 
 \draw [anchor=center, xshift= 3.3cm, yshift=-4.5 mm, rotate=-45,] (\x/19,0) -- (\x/19,0.4);

\end{tikzpicture}
\end{document}

enter image description here

share|improve this question
    
Related : tex.stackexchange.com/questions/54358/… –  percusse Aug 2 '12 at 19:52
add comment

2 Answers 2

up vote 9 down vote accepted

How about this: you define two points and the size of the parquet pattern in cm, and you'll get a rectangle definded by the two points filled with the pattern. To restrict the pattern to a non-rectangular shape, you can use the clip command, and to keep th clipping local you can put the whole construction in a scope:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{xifthen}

\newdimen\xfirst
\newdimen\yfirst
\newdimen\xsecond
\newdimen\ysecond

\newcommand{\ybig}{}
\newcommand{\ysmall}{}
\newcommand{\xbig}{}
\newcommand{\xsmall}{}

\newcommand{\parquet}[3]% first point, second point, element size in cm
{   
\begin{scope}
    \path (#1);
  \pgfgetlastxy{\xfirst}{\yfirst};
  \path (#2);
  \pgfgetlastxy{\xsecond}{\ysecond};
  \ifthenelse{\lengthtest{\yfirst > \ysecond}}%
  { \xdef\ybig{\yfirst}
    \xdef\ysmall{\ysecond}
  }%
  { \xdef\ybig{\ysecond}
    \xdef\ysmall{\yfirst}
  }
  \ifthenelse{\lengthtest{\xfirst > \xsecond}}%
  { \xdef\xbig{\xfirst}
    \xdef\xsmall{\xsecond}
  }%
  { \xdef\xbig{\xsecond}
    \xdef\xsmall{\xfirst}
  }
  \clip (\xsmall,\ysmall) rectangle (\xbig,\ybig);
  \pgfmathtruncatemacro{\xsteps}{(\xbig-\xsmall)/28.452756/#3+0.51}
  \pgfmathtruncatemacro{\ysteps}{(\ybig-\ysmall)/28.452756/#3+0.51}
  \pgfmathsetmacro{\xsmallll}{\xsmall/28.452756}
  \pgfmathsetmacro{\ysmallll}{\ysmall/28.452756}
  %\node at (0,0) {\xsteps, \ysteps};
  %\node at (0,1) {\xsmall, \ysmall};
  %\node at (0,2) {\xbig, \ybig};
  \foreach \x  in {0,...,\xsteps}
  { \foreach \y  in {0,...,\ysteps}
    {   \foreach \z in {0,...,4}
        {   \draw (\xsmallll+\x*#3-\z*#3/8,{\ysmallll+\y*#3-(4-\z)*#3/8}) -- ({\xsmallll+\x*#3+(4-\z)*#3/8},\ysmallll+\y*#3+\z*#3/8);
            \draw ({\xsmallll+(\x+0.5)*#3+\z*#3/8},\ysmallll+\y*#3+\z*#3/8) -- (\xsmallll+\x*#3+\z*#3/8,{\ysmallll+(\y+0.5)*#3+\z*#3/8});
        }
    }
  }
\end{scope}  
}

\begin{document}

\begin{tikzpicture}
    \clip[draw] (0,0) -- (1,2) -- (4,3) -- (3,1) -- (0,0);
    \parquet{0,0}{4,3}{0.5}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{scope}
        \clip[draw] (0,0) arc (180:360:3) arc (0:180:2) arc (0:180:1);
        \parquet{0,-3}{6,2}{1}
    \end{scope}
    \begin{scope}
        \clip[draw] (4,-1) circle (3 and 1);
        \parquet{1,-2}{7,0}{0.5}
    \end{scope}
\end{tikzpicture}

\end{document}

enter image description here


Edit 1: I added another parameter to control in how many 'stripes' each rectangle is divided:

\documentclass[parskip]{scrartcl}
\usepackage[margin=15mm]{geometry}
\usepackage{tikz}
\usepackage{xifthen}

\newdimen\xfirst
\newdimen\yfirst
\newdimen\xsecond
\newdimen\ysecond

\newcommand{\ybig}{}
\newcommand{\ysmall}{}
\newcommand{\xbig}{}
\newcommand{\xsmall}{}

\newcommand{\parquet}[4]% first point, second point, element size in cm, numer of subdivisions
{   
\begin{scope}
    \path (#1);
  \pgfgetlastxy{\xfirst}{\yfirst};
  \path (#2);
  \pgfgetlastxy{\xsecond}{\ysecond};
  \ifthenelse{\lengthtest{\yfirst > \ysecond}}%
  { \xdef\ybig{\yfirst}
    \xdef\ysmall{\ysecond}
  }%
  { \xdef\ybig{\ysecond}
    \xdef\ysmall{\yfirst}
  }
  \ifthenelse{\lengthtest{\xfirst > \xsecond}}%
  { \xdef\xbig{\xfirst}
    \xdef\xsmall{\xsecond}
  }%
  { \xdef\xbig{\xsecond}
    \xdef\xsmall{\xfirst}
  }
  \clip (\xsmall,\ysmall) rectangle (\xbig,\ybig);
  \pgfmathtruncatemacro{\xsteps}{(\xbig-\xsmall)/28.452756/#3+0.51}
  \pgfmathtruncatemacro{\ysteps}{(\ybig-\ysmall)/28.452756/#3+0.51}
  \pgfmathsetmacro{\xsmallll}{\xsmall/28.452756}
  \pgfmathsetmacro{\ysmallll}{\ysmall/28.452756}
  \foreach \x  in {0,...,\xsteps}
  { \foreach \y  in {0,...,\ysteps}
    {   \foreach \z in {0,...,#4}
        {   \draw (\xsmallll+\x*#3-\z*#3/2/#4,{\ysmallll+\y*#3-(#4-\z)*#3/2/#4}) -- ({\xsmallll+\x*#3+(#4-\z)*#3/2/#4},\ysmallll+\y*#3+\z*#3/2/#4);
            \draw ({\xsmallll+(\x+0.5)*#3+\z*#3/2/#4},\ysmallll+\y*#3+\z*#3/2/#4) -- (\xsmallll+\x*#3+\z*#3/2/#4,{\ysmallll+(\y+0.5)*#3+\z*#3/2/#4});
        }
    }
  }
\end{scope}  
}

\begin{document}

\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{4}
\end{tikzpicture}
\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{2}
\end{tikzpicture}

\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{8}
\end{tikzpicture}
\begin{tikzpicture}
    \clip[draw] (0,0) -- (3,1) -- (2,4) -- (-1,3) -- (0,0);
    \parquet{-1,0}{3,4}{1}{6}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
This is very elegant. I have been trying to understand the code, ie how to change the distance between lines, but so far I have not been succesful. –  Yves Jul 26 '12 at 16:10
    
Excellent and very flexible.. –  Yves Aug 2 '12 at 19:46
add comment

Not a direct answer to your question, but I had to draw a similar cable time ago. This is my solution:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\usetikzlibrary{scopes,fadings,patterns}

\begin{tikzpicture}[x=4mm,>=latex]
  \def\cilindro#1#2#3#4{%
  % #1 Diametro
  % #2 Longitud
  % #3 color
  % #4 patron
  \draw[fill=#3] (#2,0) circle(#1/2);
  \draw[#4] (#2,0) circle(#1/2);
  \fill[#3] (#2,#1/2) rectangle (0,-#1/2);
  \fill[#4] (#2,#1/2) rectangle (0,-#1/2);
  \draw[fill=#3] (0,0) circle(#1/2);
  \draw (0,#1/2) --  +(#2,0)
        (0,-#1/2) -- +(#2,0);
  }
  \cilindro{1}{7}{black!20}{shade,shading angle=180, middle color=black!10}
  {[xshift=-.5cm] \cilindro{.8}{1.25}{white}{pattern=crosshatch}}
  {[xshift=-1cm] \cilindro{.7}{1.25}{white}{fill=none}}
  {[xshift=-2cm] \cilindro{.2}{2.5}{black!60}{shade,
     bottom color=black!10,middle color=black!80, 
     shading angle=180}}
  \draw[<-] (-1.5cm,.1) -- +(-2,2) node[above] {\small N\'ucleo de cobre};
  \draw[<-] (-.7cm,0) -- +(-.5,1) node[above] {\small Aislante};
  \draw[<-] (-.1cm,0) -- +(.5,2) node[above] {\small Malla};
  \draw[<-] (1.5cm,0) -- +(1,1) node[above] {\small Funda protectora};
\end{tikzpicture}
\end{document}

Result:

Cable

Admittedly, the braid is very simplistic. I think the proper way to deal with it is to define a new pattern, and use it instead of crosshatch.

share|improve this answer
    
Very nice cable! –  Ignasi Jul 26 '12 at 13:52
    
This is very nice. In my case, I need to add stripping lengths, and is is more practical to do this on a 2D drawing. –  Yves Jul 26 '12 at 16:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.