3

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} 
  • You can use pstricks or pst-poly for regular polygons. – Bernard Dec 30 '14 at 14:37
3

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).

  • Out of curiosity, why is it preferred not to use Tikz? – Tyler Jan 5 '15 at 17:27
1

enter image description here

\documentclass{article}
\usepackage{MnSymbol,xcolor}
\begin{document}
    How about this?
    \rlap{\color{green}$\filledlargestar$}\color{red}$\largepentagram$
\end{document}
  • thanks for your input. The document that I am writing is a PhD thesis - so, this kind of symbol will not be accepted. but, I will check out the MnSymbol package. – abhishek Dec 31 '14 at 10:34
  • @abhishek It does not require symbols from specific package. Any outlined-filled-pair of symbol is OK. For instance, amsmath contains many triangles. – Symbol 1 Dec 31 '14 at 10:41
  • I am struggling badly with this. can you help me. I recently posted another question: tex.stackexchange.com/questions/220132/… can you check and help me with this? – abhishek Dec 31 '14 at 10:45

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.