3

I am generating a hexagon via this code obtained from Harvey Sheppard:

\DeclareDocumentCommand{\hexagon}{O{Black} m m O{}}{
    \fill [#1, #4] ($ (#2) + (0:#3) $) -- ($ (#2) + (60:#3) $) -- ($ (#2) + (120:#3) $) -- ($ (#2) + (180:#3) $) -- ($ (#2) + (240:#3) $) -- ($ (#2) + (300:#3) $) -- cycle;
}

How to add a thick red outline to this hexagon?

3
  • 2
    Please don't post snippets. Instead, put your fragments into a complete compilable document that shows the problem.
    – Thruston
    Jan 4, 2022 at 22:20
  • 1
    The command would probably be more flexible if it used \draw with a fill option...
    – Thruston
    Jan 4, 2022 at 22:24
  • @Thruston, can you post an example of your suggestion? Thanks!
    – Joe
    Jan 5, 2022 at 3:20

3 Answers 3

4

It's hard to tell from the context how you plan to use this, but here is a command \myhex that takes two arguments, one optional.

\myhex[<redthickness>]{<radius>}

Default redthickness is 1.5mm.

enter image description here

\documentclass{article}

\usepackage{tikz}

\newcommand{\myhex}[2][1.5mm]{\draw[red, line width=#1,fill=black](0:#2)--(60:#2)--(120:#2)--(180:#2)--(240:#2)--(300:#2)--cycle;}

\begin{document}

\begin{tikzpicture}
\myhex{1cm}\begin{scope}[xshift=2.5cm]\myhex[2mm]{8mm}\end{scope}
\end{tikzpicture}

\end{document}
7

I would recommend using shapes.geometric from tikz for this:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{tikzpicture}
    \node[regular polygon, 
    draw, 
    regular polygon sides = 6, 
    minimum size = 2in,
    fill=black, 
    draw=red,
    line width=6pt,
    ] (p) at (0,0) {};
\end{tikzpicture}
\end{document}

enter image description here

2

If you can compile your document with lualatex, then you could also use this Metapost alternative. The luamplib package lets you include MP code as part of your macro definitions, with direct access to the usual macro parameters.

\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\newcommand{\rhex}[2][0.5]{\ensuremath{\vcenter{\begin{mplibcode}beginfig(0);
path p; p = for i=0 upto 5: (#2, 0) rotated 60i -- endfor cycle;
fill p withcolor 3/4[red, white]; 
draw p withpen pencircle scaled #1 withcolor 2/3 red;
endfig;\end{mplibcode}}}}
\begin{document}
In normal text: \rhex{5pt}.
Inside maths mode: $\rhex[1]{5pt} + \left(\rhex7\right)^2$
\end{document}

Here I have defined a command that will work in text mode or math mode. If you compile this with lualatex you should get something like this:

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .