This is a related (more general) question to the previous post

Tikz: Drawing a line inside of certain shape

The answer given in the above question works perfectly if the given shape can be described by a single path. However, it doesn't work with any shape containing 2x2 block.

Let me illustrate my problem with the following example.

enter image description here

Like the picture above, I have a connected shape which is a union of square blocks. Now, I want to draw red lines so that these red lines form a "shrinked" version of the shape. However, I can not simply scale the shape as the new shape fomed by red lines is not exactly similar to the original shape.

Here is my code:

        \draw (-3, -3) grid (5, 5);
        \draw[dashed, color=red] (0+0.1, 0-0.1) -- (1+0.1, 0-0.1) -- (1+0.1, 2-0.1)-- (2+0.1, 2-0.1) -- (2+0.1, 4-0.1) -- (3-0.1, 4-0.1) -- (3-0.1, 1+0.1) -- (3-0.1, -1+0.1) -- (-0+0.1, -1+0.1) -- (0+0.1, 0-0.1);

Although I can manually add or substract a small number to every vertex of the original shape to create red lines, but I am looking for a simpler solution. Thanks in advance.


An option using some fake shape, using a line inside other with less thicknes that not allow to draw like dashed style, whatever is posible with manual drawing helped using node names for desired shapes and without style problems.

RESULT: enter image description here


            line width=0.8cm,
            shorten >=-0.4cm,
            shorten <=-0.4cm,
                -,draw,white, line width=0.75cm, shorten >=-0.375cm,
            shorten <=-0.375cm,
    \draw (0,0) grid (8, 8);
    \foreach \x in {1,2,...,8}{
        \foreach \y in {1,2,...,8}{
                \draw (\x-0.5,\y-0.5) coordinate (\x-\y);
                \node[rectangle, inner sep=12pt](N-\x-\y) at (\x-0.5,\y-0.5){};
        %Draw as a line % dasheed is not posible
        \begin{scope}[on background layer]
        \draw[Myline] (1-1) |- (2-3) |- (1-5);
        \draw[Myline] (4-3) -| (5-5) -| (6-7);
        \draw[Myline,blue] (4-1) -| (5-2) -| (6-1) -| (8-2) -| (7-3);
        %Manual drawing using node coordinates
        \draw[dashed,red,thick,fill=red,fill opacity=0.1](N-1-8.135) 
            -| (N-5-6.-45)
            -| (N-4-7.-135)
            -| (N-3-7.135) 
            -| (N-5-8.-135)
            -| cycle;
        \draw[dashed,blue,thick,fill=blue,fill opacity=0.1](N-4-3.-135)
            -| (N-6-7.45)
            -| (N-6-5.135)
            -| (N-5-3.135) 
            -| cycle;

This is really just a fun proposal, which may or may not be useful. It seems to me that you want to draw a boundary around certain points of the grid. We could represent the grid by a matrix and say that the boundary should be drawn around the entries 1, all other entries are 0. Here is a very first version. It can and will be improved if that way turns out to make at least a tiny bit of sense. At the moment, the dimensions are hard coded and the shape cannot extend to the boundary of the grid, two things that can easily be fixed.


\pgfplotstableread[header=false,col sep=comma]{grid.csv}\datatable
\foreach \Y [evaluate=\Y as \PrevY using {int(\Y-1)},
evaluate=\Y as \NextY using {int(\Y+1)}] in {1,...,6}
{\foreach \X [evaluate=\X as \PrevX using {int(\X-1)},
evaluate=\X as \NextX using {int(\X+1)}] in {1,...,6}
   \draw[mystyle] (1+\X-0.95,8-\Y-0.95) -- (1+\X-0.95,8-\Y-0.05);   
   \draw[mystyle] (1+\X-0.05,8-\Y-0.95) -- (1+\X-0.05,8-\Y-0.05);   
   \draw[mystyle] (1+\X-0.95,8-\Y-0.05) -- (1+\X-0.05,8-\Y-0.05);   
   \draw[mystyle] (1+\X-0.95,8-\Y-0.95) -- (1+\X-0.05,8-\Y-0.95);   
\draw (0,0) grid (8,8);

enter image description here


I don't know if this answer will fit the problem, but it shows another way for referencig cells and filling areas.

logicpuzzle is package which can be used to prepare what it's name means, logic puzzles loke sudokus, kakuros, ... But it also provides an easy way of drawing grids and some commands to frame and fill areas in these grids.

Environment \begin{logicpuzzle}[rows=8,columns=8]...\end{logicpuzzle} will draw a 8x8 grid.

Commands for drawing/filling areas inside the grid are \framearea{color}{tikzpath}, \fillarea{color}{tikzpath}, \colorarea{color}{tikzpath}. In this commands the tikzpath is path where coordinates identifiy the lower left corner of each cell and where (1,1) is the lower left cell. Linear (--) or perpendicular (-|, |-) paths are allowed.

Another syntax is provided by xtikzpath which can be used inside tikzpath argument in previous commands.


fills cell (6,1). First ,two arguments represent the bottom left corner of starting position. Following pairs like 6/1 mean move right (6=right) 1 cell. The first number of the pair represents a direction according its position in numeric keyboard, while the second is the distance to move.

A complete code with some examples looks like:


enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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