[Tex/LaTex] Creating shapes like circle, rotated squares, and triangle without tikz

colordiagramsshapes

In the previous post, I got help with creating boxes with black borders and custom fills.

How can I create other shapes like triangles, circles, rotated squares, etc. with custom fills.

I do not want to use tikz at this moment.

\documentclass[11pt,a4paper]{article}%
\usepackage{xcolor}

% some color definitions
\definecolor{cblue}{RGB}{16,78,139}
\definecolor{cred}{RGB}{139,37,0}
\definecolor{cgreen}{RGB}{0,139,0} 

% normal box
\newcommand{\sqboxs}{1.2ex}% the square size
\newcommand{\sqboxf}{0.6pt}% the border in \sqboxEmpty
\newcommand{\sqbox}[1]{\textcolor{#1}{\rule{\sqboxs}{\sqboxs}}}
\newcommand{\sqboxblack}[1]{\setlength{\fboxsep}{0pt}\fbox{\sqbox{#1}}}

% empty box
\newcommand{\sqboxEmpty}[1]{%
  \begingroup
  \setlength{\fboxrule}{\sqboxf}%
  \setlength{\fboxsep}{-\fboxrule}%
  \textcolor{#1}{\fbox{\rule{0pt}{\sqboxs}\rule{\sqboxs}{0pt}}}%
  \endgroup
}

\newcommand{\sqboxEmptyblack}[1]{\setlength{\fboxsep}{0pt}\fbox{\sqboxEmpty{#1}}}

\begin{document}
I like these.
\sqbox{cred}  \sqbox{cgreen} \sqbox{cblue}

And also these.
\sqboxEmpty{cred}  \sqboxEmpty{cgreen}  \sqboxEmpty{cblue}

\end{document} 

Best Answer

I approve of your decision not to use TikZ. You can create various shapes by low level PDF commands, as is used in my code here. We define \sqbox for square, \trianbox for triangle, \uptrianbox for rotated triangle, \circbox for circle and \diabox for diamonds. All these macros have two parameters: first one is 0 if we need outlined shape or 1 if we need solid shape. Second one is the color defined by \def\nameCOLOR.

\def\sqPDF#1#2{0 0 m #1 0 l #1 #1 l 0 #1 l h}
\def\trianPDF#1#2{0 0 m #1 0 l #2 4.5 l h}
\def\uptrianPDF#1#2{#2 0 m #1 4.5 l 0 4.5 l h}
\def\circPDF#1#2{#1 0 0 #1 #2 #2 cm .1 w .5 0 m
   .5 .276 .276 .5 0 .5 c -.276 .5 -.5 .276 -.5 0 c
   -.5 -.276 -.276 -.5 0 -.5 c .276 -.5 .5 -.276 .5 0 c h}
\def\diaPDF#1#2{#2 0 m #1 #2 l #2 #1 l 0 #2 l h}

\def\credCOLOR   {.54 .14 0}
\def\cblueCOLOR  {.06 .3 .54}
\def\cgreenCOLOR {0 .54 0}

\def\genbox#1#2#3#4#5#6{% #1=0/1, #2=color, #3=shape, #4=raise, #5=width, #6=width/2
    \leavevmode\raise#4bp\hbox to#5bp{\vrule height#5bp depth0bp width0bp
    \pdfliteral{q .5 w \csname #2COLOR\endcsname\space RG
                       \csname #3PDF\endcsname{#5}{#6} S Q
             \ifx1#1 q \csname #2COLOR\endcsname\space rg 
                       \csname #3PDF\endcsname{#5}{#6} f Q\fi}\hss}}

                                    % shape     raise width  width/2
\def\sqbox      #1#2{\genbox{#1}{#2}  {sq}       {0}   {4.5}  {2.25}}
\def\trianbox   #1#2{\genbox{#1}{#2}  {trian}    {0}   {5}    {2.5}}
\def\uptrianbox #1#2{\genbox{#1}{#2}  {uptrian}  {0}   {5}    {2.5}}
\def\circbox    #1#2{\genbox{#1}{#2}  {circ}     {0}   {5}    {2.5}}
\def\diabox     #1#2{\genbox{#1}{#2}  {dia}      {-.5} {6}    {3}}

%% usage:

squares: \sqbox0{cgreen}, \sqbox1{cred}, \sqbox0{cblue}.

triangles: \trianbox0{cgreen}, \trianbox1{cred}, \trianbox0{cblue}.

triangles: \uptrianbox0{cgreen}, \uptrianbox1{cred}, \uptrianbox0{cblue}.

circles: \circbox0{cgreen}, \circbox1{cred}, \circbox0{cblue}.

diamonds: \diabox0{cgreen}, \diabox1{cred}, \diabox0{cblue}.

The result:

shapes

You need not any package for this. Nor xcolor. All stuff is done at PDF low level commands. If you are using XeTeX (no pdfTeX) then you need to define \pdfliteral by:

\def\pdfliteral#1{\special{pdf:literal #1}}

I tried this in plain TeX but I hope that the code will work in LaTeX too (after adding \documentclass etc. sequences).