5

How can I draw a rectangle with a 'double-border', which allows to specify a color for each of its 8 (4 inner, 4 outer) borders separately and does not widen the separating space between the outer and inner rectangle when scaled in a resizebox?

(Sorry, I'm very new to TikZ.)

I tried the following, its shortcomings are: its 3 'rectangles' (inner border equals outer border) and its edges look 'not connected':

\begin{tikzpicture}
  \draw[red, double = black] (0,0) -- (1,0);
  \draw[green, double = purple] (1,0) -- (1,1);
  \draw[blue, double = brown] (1,1) -- (0,1);
  \draw[yellow, double = white] (0,1) -- (0,0);
\end{tikzpicture}

Here is a picture to illustrate the problem

MCH
  • 377
  • 2
    Example code showing what you have tried, what doesn't work, and maybe a picture of what you are trying to achieve would go a long way to help those trying to help you. – Steven B. Segletes May 07 '14 at 19:06
  • I think this question is related with your previous one but I still don't understand what you want to do. Could you post any example (even hand-drawn) showing it? Why do you need to resize it while keeping a constant distance between borders? – Ignasi May 07 '14 at 19:36
  • Sorry, I guess this question was really unclear ... tried my best to clarify ... – MCH May 07 '14 at 20:17
  • Do you want your tikz figure being resized to fill right area? keeping proportions? just vertically centered? – Ignasi May 07 '14 at 22:28

1 Answers1

7

Maybe that ?

enter image description here

\documentclass{article}
\usepackage{tikz,lipsum}
\usetikzlibrary{calc}

\usepackage[marginparsep=3pt, top=2cm, bottom=1.5cm, left=1.5cm, right=1.5cm]{geometry}

\makeatletter
\newcommand{\Square}[2]{%
    \def\@W{#1}%
    \begin{tikzpicture}[baseline=(current bounding box.base)]
    \foreach \o/\i [count=\j from 0] in {
        blue/red,
        yellow/black,
        orange/gray,
        green/violet}{%
    \fill[rotate=\j*90,\o]
        (-#2,-#2)--(#2,-#2)--(#2-\@W,\@W-#2)--(\@W-#2,\@W-#2)--cycle ;
    \fill[rotate=\j*90,\i]
        (-#2+\@W,-#2+\@W)--(#2-\@W,-#2+\@W)--(#2-2*\@W,2*\@W-#2)--(2*\@W-#2,2*\@W-#2)--cycle ;
        }
    \end{tikzpicture}
}
\makeatother


\newlength{\MyHeight}
\newsavebox{\MyBox}

\newcommand{\DBbox}[3][.05]{%
    \begin{lrbox}{\MyBox}
    \fbox{%
        \begin{minipage}{#2}
        #3
        \end{minipage}  
    }%
    \end{lrbox}%
    \settoheight{\MyHeight}{\usebox{\MyBox}}%
    \addtolength{\MyHeight}{-.2\baselineskip}% <- WHY ???
    \usebox{\MyBox}%
    \raisebox{.2\baselineskip}{\Square{#1}{\MyHeight}}% <- WHY ???
    }

\begin{document}

\DBbox[5pt]{9cm}{%
    \lipsum[1]}

\bigskip 

\DBbox[20pt]{7cm}{%
    \lipsum[1]}

\end{document}

More stuff :

enter image description here

\documentclass[margin=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\newcommand{\Square}[5][%
        blue/red,
        yellow/black,
        orange/gray,
        green/violet%
        ]{%
        \pgfpointdiff{\pgfpointanchor{#3}{center}}
                 {\pgfpointanchor{#4}{center}}
        \pgf@xa=\pgf@x
        \pgf@ya=\pgf@y
        \pgfmathparse{veclen(\pgf@xa,\pgf@ya)/2.82843}
        \let\@L\pgfmathresult
    \def\@W{#2}% epaisseur
    %\def\@L{#3}% côté
    \begin{scope}[shift={($(#3)!.5!(#4)$)}]
    \node {#5} ;
    \foreach \o/\i [count=\j from 0] in {#1}{%
    \fill[rotate=\j*90,\o]
        (-\@L pt,-\@L pt)--(\@L pt,-\@L pt)--(\@L-\@W,\@W-\@L)--(\@W-\@L,\@W-\@L)--cycle ;
    \fill[rotate=\j*90,\i]
        (-\@L+\@W,-\@L+\@W)--(\@L-\@W,-\@L+\@W)--(\@L-2*\@W,2*\@W-\@L)--(2*\@W-\@L,2*\@W-\@L)--cycle ;
        }       
    \end{scope}
}
\makeatother


\begin{document}
\begin{tikzpicture}

\coordinate (A) at (0,0) ;
\coordinate (B) at (5,5) ;
\coordinate (C) at (3,-3) ;

    \Square{8pt}{A}{B}{Text 2} ;
    \Square[
        red/orange,
        yellow!30!green/gray,
        blue!25/black,
        violet/green%   
    ]{5pt}{A}{C}{Text 1} ;
\end{tikzpicture}
\end{document}
Tarass
  • 16,912
  • 1
    \xdef\W{#1}? Why? I can see no reason for it (and several for avoiding it). – egreg May 07 '14 at 23:09
  • It is a relic from something else but in fact #2+\W is easier read than #2+#1. What are the reasons to avoid it? – Tarass May 07 '14 at 23:24
  • I don't think so; and globally defining \W is definitely not a good idea. – egreg May 07 '14 at 23:25
  • I made the changes. I understand the problem with the global, but how to define locally ? – Tarass May 07 '14 at 23:31
  • 2
    There's no reason for it; you should use a control sequence that is not likely to be used elsewhere (this is what \makeatletter is for) or enclose the whole working in a group. – egreg May 07 '14 at 23:32
  • The rectangle itself looks exactly what I want it to. I am currently trying to alter your solution to have different rectangles like this (but with a text label) next to each other by defining a command which gets two coordinates and draws a corresponding rectangle in the right area. – MCH May 08 '14 at 17:18
  • Well you see, I am very bad at tikz and I would gladly give you my 150 rep (How can I do that? Bounty?) for a command which draws a rectangle looking like yours between two coordinates in a tikzpicture and has a text label. Something like `\drect{0,0}{10,10}{1pt}{Label}{blue,yellow,orange,green,red,black,grey,violet}'. I need this to draw multiple of these rectangles next to each other in the right area. Thank you for your solution, it is a step in the right direction I was not able to perform. – MCH May 08 '14 at 18:07
  • Where do you want the label text ? – Tarass May 08 '14 at 18:22
  • Hi! It should be centered inside the rectangle. Thank you! – MCH May 08 '14 at 18:25
  • It's done, but be sure that your two point are two opposite corner of a square. – Tarass May 08 '14 at 18:56
  • Man, this is perfect! :D Many thanks!! As for the 150 rep I promised you: Can we do it like this (?): I wait until I can set a bounty on the question and then accept your answer? (Sorry I'm new to Stackoverflow --- Does it work this way?) – MCH May 08 '14 at 19:00
  • Keep your rep, just click on the tick near the 3 at the top of my answer. Glad taht I could help you, I learned working on your question. – Tarass May 08 '14 at 19:31
  • Nah, I keep my promises, just have to wait 21 hours until I can set the bounty. Thank you so much for your answer :) – MCH May 08 '14 at 21:06