DEV Community

Build a Star-Rated Example Box in LaTeX

Hi, I'm KKTeX. I publish LaTeX packages on CTAN and build typesetting tools for education in Japan.

A Japanese exam-prep page often tells you three things before the problem begins: its number, its topic, and its difficulty. That compact metadata turns a dense booklet into something students can scan and revisit.

Today we will make one component: a worked-example box with two measured header segments and a three-star difficulty scale.

A measured ExampleBox for a quadratic-inequality problem

The locator above the box and the bold problem statement are page content from my figure source, not automatic parts of ExampleBox.

Measure first, then draw

\usepackage[most]{tcolorbox}
\usetikzlibrary{shapes.geometric}

\definecolor{qink}{HTML}{0F172A}         % slate-900
\definecolor{qline}{HTML}{CBD5E1}        % slate-300
\definecolor{qaccentDark}{HTML}{0369A1}  % sky-700
\definecolor{qaccentPale}{HTML}{E0F2FE}  % sky-100

\newlength{\ExampleHeadHeight}
\newlength{\ExampleTitleWidth}
\newlength{\ExampleTopicWidth}
\newlength{\ExampleTopicX}
\setlength{\ExampleHeadHeight}{6.2mm}

\newcommand{\ExampleSegmentFont}{%
  \sffamily\bfseries\fontsize{8.5}{10}\selectfont
}

\DeclareTColorBox{ExampleBox}{ m m o O{2} O{} }{%
  enhanced,
  empty,
  sharp corners,
  before={%
    \def\ExampleTitleText{%
      #1%
      \IfValueT{#3}{%
        \hspace{1.5mm}{\fontsize{6}{7}\selectfont #3}%
      }%
    }%
    \settowidth{\ExampleTitleWidth}{%
      \ExampleSegmentFont\ExampleTitleText
    }%
    \settowidth{\ExampleTopicWidth}{%
      \ExampleSegmentFont #2
    }%
    \setlength{\ExampleTopicX}{%
      \dimexpr\ExampleTitleWidth+7.7mm\relax
    }%
    \par\noindent
  },
  enlarge top by=\ExampleHeadHeight,
  left=5mm, right=3mm, top=3.5mm, bottom=3.5mm,
  underlay unbroken={%
    % Measured title segment
    \fill[qaccentDark]
      ([yshift=\ExampleHeadHeight]frame.north west)
      -- ([xshift=\dimexpr\ExampleTitleWidth+6.5mm\relax,
           yshift=\ExampleHeadHeight]frame.north west)
      -- ([xshift=\dimexpr\ExampleTitleWidth+6.5mm
                         -.5\ExampleHeadHeight\relax]
           frame.north west)
      -- (frame.north west)
      -- cycle;
    \node[anchor=west,inner sep=0pt,text=white,
          font=\ExampleSegmentFont]
      at ([xshift=2.5mm,yshift=.5\ExampleHeadHeight]
          frame.north west)
      {\ExampleTitleText};

    % Measured topic segment with the same shear
    \fill[qaccentPale]
      ([xshift=\ExampleTopicX,yshift=\ExampleHeadHeight]
        frame.north west)
      -- ([xshift=\dimexpr\ExampleTopicX+\ExampleTopicWidth
                         +5mm\relax,
           yshift=\ExampleHeadHeight]frame.north west)
      -- ([xshift=\dimexpr\ExampleTopicX+\ExampleTopicWidth
                         +5mm-.5\ExampleHeadHeight\relax]
           frame.north west)
      -- ([xshift=\dimexpr\ExampleTopicX
                         -.5\ExampleHeadHeight\relax]
           frame.north west)
      -- cycle;
    \node[anchor=west,inner sep=0pt,text=qink,
          font=\ExampleSegmentFont]
      at ([xshift=\dimexpr\ExampleTopicX+2.5mm
                         -.25\ExampleHeadHeight\relax,
           yshift=.5\ExampleHeadHeight]frame.north west)
      {#2};

    % Right-anchored stars; #4 is the filled count.
    \foreach \i in {1,2,3}{%
      \ifnum\i>#4\relax
        \node[star,star points=5,star point ratio=2.2,
          minimum size=2.6mm,inner sep=0pt,
          fill=white,draw=qline,line width=.4pt]
          at ([xshift={-2.3mm-(3-\i)*3.4mm},
               yshift=.5\ExampleHeadHeight]
               frame.north east) {};
      \else
        \node[star,star points=5,star point ratio=2.2,
          minimum size=2.6mm,inner sep=0pt,
          fill=qaccentDark,draw=qaccentDark,line width=.4pt]
          at ([xshift={-2.3mm-(3-\i)*3.4mm},
               yshift=.5\ExampleHeadHeight]
               frame.north east) {};
      \fi
    }%

    % Open frame: a strong spine and three quiet rules.
    \draw[qink,line width=1.5pt]
      ([xshift=.75pt,yshift=\ExampleHeadHeight]
        frame.north west)
      -- ([xshift=.75pt]frame.south west);
    \draw[qink,line width=.5pt]
      ([yshift=\ExampleHeadHeight]frame.north west)
      -- ([yshift=\ExampleHeadHeight]frame.north east);
    \draw[qink,line width=.5pt]
      (frame.north west) -- (frame.north east);
    \draw[qink,line width=.5pt]
      (frame.south west) -- (frame.south east);
  },
  #5
}
Enter fullscreen mode Exit fullscreen mode

The normal call is portable:

\begin{ExampleBox}{EXAMPLE 07}{Quadratic Inequalities}
Find all real \(a\) such that
\[
  x^2+ax+a+3>0
\]
for every real \(x\).
\end{ExampleBox}
Enter fullscreen mode Exit fullscreen mode

The first optional argument adds a LuaLaTeX Japanese label; the second sets the filled-star count:

\begin{ExampleBox}
  {EXAMPLE 07}{Quadratic Inequalities}[例題][2]
  ...
\end{ExampleBox}
Enter fullscreen mode Exit fullscreen mode

The important rule is simple: measure with the exact font used to draw. Both polygons then grow with their text, while \ExampleHeadHeight controls the band and both parallel shears.

Why the strategy appears before the answer

Japanese self-study books often separate the way in from the full solution. For example, the exam-prep series described by Suken Publishing places a 指針 (strategy) before each solution and uses side notes for formulas and extra explanation (publisher release).

That is why the preview says STRATEGY before calculating (D<0). The line is ordinary content inside the same box—not a second component.

This measured-header pattern grew from KKfbox11 and KKfbox8 in my private kkbookmaker toolkit. I kept the genre's information hierarchy but replaced its proportions, colors, glyph stars, and frame construction.

This design and its proportions are new for this series. It is not affiliated with, and does not copy, Suken Publishing, a particular textbook series, or any school.

Two tools from the same desk

Disclosure: I'm promoting both projects in this section.

Evolton interface and solution-PDF examples

Evolton turns a photo or screenshot of a mathematics problem into a polished, reference-book-style solution PDF. Its database is browsable by university, faculty, and year.

MathHover LaTeX input panel

MathHover opens a LaTeX panel beside your cursor and pastes the finished formula into the app you are already using. It is free and requires no account.

Next: a section heading with a ghost number.

Top comments (0)