10

I want to mimic a calculator inputs and display with latex, after looking at several packages like tipfr and ticollege and finding them unsatisfactory to my needs I decided to do it myself with tcolorbox, and piecing together a few answers from here. Here's a MWE:

\documentclass[openany,10pt]{book}

\usepackage{newpxtext}

\usepackage[varg]{newpxmath} %font package

\usepackage[letterpaper,margin=0.75in,bindingoffset=0.5in]{geometry}

\usepackage[most]{tcolorbox}
    \colorlet{blackened}{black!90!white}
    \colorlet{blackish}{black!70!white}
    \colorlet{greyish}{black!60!white}
    \colorlet{whiteish}{white}
    \colorlet{orangeish}{yellow!90!red}
    \colorlet{greenish}{green!16!gray}
    \colorlet{redish}{red!80!black}

\tcbset{calbackground/.style={
    enhanced, 
    leftright skip=0.25cm,beforeafter skip=0pt,
    toptitle=0mm,bottomtitle=0mm,
    right=2mm,left=2mm, 
    top=1pt,
    bottom=0.25cm,
    boxsep=0pt,
    boxrule=0mm, 
    sharp corners,
    sidebyside,
    sidebyside gap=2mm,
    lefthand ratio=0.6,
    bicolor,
    colback=black!10!white, 
    colbacklower=greenish,
    colframe=white, 
    autoparskip,
    }}

\newtcbtheorem[no counter]{calx}{Calculator}{calbackground}{cax}

\newtcbox{\KY}[1][]{
    enhanced,
    on line,
    arc=2pt,outer arc=2pt,
    boxrule=0pt,bottomrule=0.25mm,rightrule=0.2mm,
    boxsep=0pt,left=0pt,right=0pt,top=1pt,bottom=1pt,
    interior style={top color=blackish,bottom color=blackened},
    colframe=greyish,
    width=2.5em,
    tcbox width=forced center,
    equal height group=K,
    valign=center,
    fontupper=\footnotesize\sffamily,
    coltext=orangeish,}
%   coltitle=redish,colbacktitle=white,fonttitle=\footnotesize\sffamily,squeezed title=#1,center title,}

\newtcbox{\KN}{
    enhanced,
    on line,
    arc=2pt,outer arc=2pt,
    boxrule=0pt,bottomrule=0.25mm,rightrule=0.2mm,
    boxsep=0pt,left=0pt,right=0pt,top=1pt,bottom=1pt,
    interior style={top color=blackish,bottom color=blackened},
    colframe=greyish,
    width=1.5em,
    tcbox width=forced center,
    equal height group=K,
    valign=center,
    fontupper=\footnotesize\sffamily,
    coltext=whiteish,}

\usepackage{lcd}
    \LCDcolors{black}{greenish}
    \DefineLCDchar{sq}{11100001000100011100000000000000000}
    \DefineLCDchar{tm}{00000100010101000100010101000100000}
    \DefineLCDchar{dv}{00000001000000011111000000010000000}
    \DefineLCDchar{mu}{00011000011110100001000000000000000}
    \DefineLCDchar{sc}{11011010011001000000000000000000000}

\DeclareMathOperator{\ee}{\textrm{e}}

\usepackage{MnSymbol} %boxes
\usepackage{xfrac} %sfrac
\usepackage{lipsum} %for textwidth comparison
    \setlipsumdefault{1-2}

\begin{document}

\begin{calx}{}{} 
\KY{$($}\KN{3}\KY{$x^2$}\KN{-}\KN{4}\KY{$x^2$}\KN{-}\KN{6}\KY{$x^2$}\KY{$)$}\KN{$=$}
\tcblower
{\Large\textLCD[0]{20}|(3{sq}-4{sq}-6{sq})|}
{\LARGE\textLCD[0]{16}|             -43|}
\end{calx}
%
\begin{calx}{}{} 
\KN{$\div$}\KY{$($}\KY{$(-)$}\KN{2}\KN{$\times$}\KN{4}\KN{$\times$}\KN{6}\KY{$)$}\KN{$=$}
\tcblower
{\Large\textLCD[0]{20}|ANS{dv}(-2{tm}4{tm}6)|}
{\LARGE\textLCD[0]{16}|    0.8958333333|}
\end{calx}
%
\begin{calx}{}{} 
\KY{$\cos^{-1}$}\KY{ANS}\KN{$=$}
\tcblower
{\Large\textLCD[0]{20}|cos{mu}(ANS|}
{\LARGE\textLCD[0]{16}|     26.38432975|}
\end{calx}
%
\begin{calx}{}{} 
\KY{$\circ\,\prime\,\prime\prime$}
\tcblower
{\Large\textLCD[0]{20}|cos{mu}(ANS|}
{\LARGE\textLCD[0]{16}|     26°23'3.59{sc} |}
\end{calx}

\lipsum

\KY{$x^2$}%
\KY{$x^3$}%
\KY{$x^{-1}$}%
\KY{$x^{\filledsquare}$}%
\KY{$\sqrt{\filledmedsquare}$}%
\KY{$\sqrt[3]{\filledmedsquare}$}%
\KY{$\sqrt[\filledsquare]{\medsquare}$}\\

\KY{$\ln$}%
\KY{$\log$}%
\KY{$\log_{\filledsquare}\medsquare$}%
\KY{$10^{\filledsquare}$}%
\KY{$\ee^{\filledsquare}$}\\

\KY{$(-)$}%
\KY{$\frac{\filledmedsquare}{\medsquare}$}%
\KY{$\sfrac{\filledmedsquare}{\medsquare}$}%
\KY{$\tfrac{\filledmedsquare}{\medsquare}$}\\

\KY{$\circ\,\prime\,\prime\prime$}%
\KY{$\sin$}%
\KY{$\sin^{-1}$}%
\KY{$\cos$}%
\KY{$\cos^{-1}$}%
\KY{$\tan$}%
\KY{$\tan^{-1}$}\\

\KY{$($}%
\KY{$)$}%
\KN{$\times$}%
\KN{$\div$}%
\KN{$+$}%
\KN{$-$}%
\KN{$=$}\\

\KN{$1$}%
\KN{$2$}%
\KN{$3$}%
\KN{$4$}%
\KN{$5$}%
\KN{$6$}%
\KN{$7$}%
\KN{$8$}%
\KN{$9$}%
\KN{$0$}\\

\KN{1}%
\KN{2}%
\KN{3}%
\KN{4}%
\KN{5}%
\KN{6}%
\KN{7}%
\KN{8}%
\KN{9}%
\KN{0}\\

\end{document}

enter image description here

This looks and works almost exactly the way I need it to, but I've a few difficulties:

1) The keys don't all align on the same line

2) The text inside different keys doesn't align on the same line

3) I can't figure out how to add a second function over a key, like sin^{-1} over the sin key, while also solving the first two problems.

Any help would be appreciated.

2 Answers 2

7

I can add some modifications to the answer of Rmano. The \strut or something similar can be put directly into the definition of \KY and \KN using the before upper option, e.g.

before upper=\vrule width 0pt height 2ex depth 1ex\relax,

I noticed that you used \KY{$...$} in most cases. The $ signs also can be put into the definition of \KY and \KN. I made a copy of \KY as \KYM which is

\newtcbox{\KYm}[1][]{
    enhanced,
    on line,
    arc=2pt,outer arc=2pt,
    boxrule=0pt,bottomrule=0.25mm,rightrule=0.2mm,
    boxsep=0pt,left=0pt,right=0pt,top=1pt,bottom=1pt,
    interior style={top color=blackish,bottom color=blackened},
    colframe=greyish,
    width=2.5em,
    tcbox width=forced center,
    equal height group=K,
    valign=center,
    fontupper=\footnotesize\sffamily,
    coltext=orangeish,
    before upper=\vrule width 0pt height 2ex depth 1ex\relax$,
    after upper=$,
    }

Now, the application is \KYm{...} to set something in mathematical mode.

enter image description here

The full code is:

\documentclass[openany,10pt]{book}

\usepackage{newpxtext}

\usepackage[varg]{newpxmath} %font package

\usepackage[letterpaper,margin=0.75in,bindingoffset=0.5in]{geometry}

\usepackage[most]{tcolorbox}
    \colorlet{blackened}{black!90!white}
    \colorlet{blackish}{black!70!white}
    \colorlet{greyish}{black!60!white}
    \colorlet{whiteish}{white}
    \colorlet{orangeish}{yellow!90!red}
    \colorlet{greenish}{green!16!gray}
    \colorlet{redish}{red!80!black}

\tcbset{calbackground/.style={
    enhanced,
    leftright skip=0.25cm,beforeafter skip=0pt,
    toptitle=0mm,bottomtitle=0mm,
    right=2mm,left=2mm,
    top=1pt,
    bottom=0.25cm,
    boxsep=0pt,
    boxrule=0mm,
    sharp corners,
    sidebyside,
    sidebyside gap=2mm,
    lefthand ratio=0.6,
    bicolor,
    colback=black!10!white,
    colbacklower=greenish,
    colframe=white,
    autoparskip,
    }}

\newtcbtheorem[no counter]{calx}{Calculator}{calbackground}{cax}

\newtcbox{\KY}[1][]{
    enhanced,
    on line,
    arc=2pt,outer arc=2pt,
    boxrule=0pt,bottomrule=0.25mm,rightrule=0.2mm,
    boxsep=0pt,left=0pt,right=0pt,top=1pt,bottom=1pt,
    interior style={top color=blackish,bottom color=blackened},
    colframe=greyish,
    width=2.5em,
    tcbox width=forced center,
    equal height group=K,
    valign=center,
    fontupper=\footnotesize\sffamily,
    coltext=orangeish,
    before upper=\vrule width 0pt height 2ex depth 1ex\relax,
    }

\newtcbox{\KYm}[1][]{
    enhanced,
    on line,
    arc=2pt,outer arc=2pt,
    boxrule=0pt,bottomrule=0.25mm,rightrule=0.2mm,
    boxsep=0pt,left=0pt,right=0pt,top=1pt,bottom=1pt,
    interior style={top color=blackish,bottom color=blackened},
    colframe=greyish,
    width=2.5em,
    tcbox width=forced center,
    equal height group=K,
    valign=center,
    fontupper=\footnotesize\sffamily,
    coltext=orangeish,
    before upper=\vrule width 0pt height 2ex depth 1ex\relax$,
    after upper=$,
    }

\newtcbox{\KN}{
    enhanced,
    on line,
    arc=2pt,outer arc=2pt,
    boxrule=0pt,bottomrule=0.25mm,rightrule=0.2mm,
    boxsep=0pt,left=0pt,right=0pt,top=1pt,bottom=1pt,
    interior style={top color=blackish,bottom color=blackened},
    colframe=greyish,
    width=1.5em,
    tcbox width=forced center,
    equal height group=K,
    valign=center,
    fontupper=\footnotesize\sffamily,
    coltext=whiteish,
    before upper=\vrule width 0pt height 2ex depth 1ex\relax,
    }

\usepackage{lcd}
    \LCDcolors{black}{greenish}
    \DefineLCDchar{sq}{11100001000100011100000000000000000}
    \DefineLCDchar{tm}{00000100010101000100010101000100000}
    \DefineLCDchar{dv}{00000001000000011111000000010000000}
    \DefineLCDchar{mu}{00011000011110100001000000000000000}
    \DefineLCDchar{sc}{11011010011001000000000000000000000}

\DeclareMathOperator{\ee}{\textrm{e}}

\usepackage{MnSymbol} %boxes
\usepackage{xfrac} %sfrac
\usepackage{lipsum} %for textwidth comparison
    \setlipsumdefault{1-2}

\begin{document}

\begin{calx}{}{}
\KY{$($}\KN{3}\KY{$x^2$}\KN{-}\KN{4}\KY{$x^2$}\KN{-}\KN{6}\KY{$x^2$}\KY{$)$}\KN{$=$}
\tcblower
{\Large\textLCD[0]{20}|(3{sq}-4{sq}-6{sq})|}
{\LARGE\textLCD[0]{16}|             -43|}
\end{calx}
%
\begin{calx}{}{}
\KN{$\div$}\KY{$($}\KY{$(-)$}\KN{2}\KN{$\times$}\KN{4}\KN{$\times$}\KN{6}\KY{$)$}\KN{$=$}
\tcblower
{\Large\textLCD[0]{20}|ANS{dv}(-2{tm}4{tm}6)|}
{\LARGE\textLCD[0]{16}|    0.8958333333|}
\end{calx}
%
\begin{calx}{}{}
\KY{$\cos^{-1}$}\KY{ANS}\KN{$=$}
\tcblower
{\Large\textLCD[0]{20}|cos{mu}(ANS|}
{\LARGE\textLCD[0]{16}|     26.38432975|}
\end{calx}
%
\begin{calx}{}{}
\KY{$\circ\,\prime\,\prime\prime$}
\tcblower
{\Large\textLCD[0]{20}|cos{mu}(ANS|}
{\LARGE\textLCD[0]{16}|     26°23'3.59{sc} |}
\end{calx}

\lipsum

\KYm{x^2}%
\KYm{x^3}%
\KYm{x^{-1}}%
\KYm{x^{\filledsquare}}%
\KYm{\sqrt{\filledmedsquare}}%
\KYm{\sqrt[3]{\filledmedsquare}}%
\KYm{\sqrt[\filledsquare]{\medsquare}}\\

\KY{$\ln$}%
\KY{$\log$}%
\KY{$\log_{\filledsquare}\medsquare$}%
\KY{$10^{\filledsquare}$}%
\KY{$\ee^{\filledsquare}$}\\

\KY{$(-)$}%
\KY{$\frac{\filledmedsquare}{\medsquare}$}%
\KY{$\sfrac{\filledmedsquare}{\medsquare}$}%
\KY{$\tfrac{\filledmedsquare}{\medsquare}$}\\

\KY{$\circ\,\prime\,\prime\prime$}%
\KY{$\sin$}%
\KY{$\sin^{-1}$}%
\KY{$\cos$}%
\KY{$\cos^{-1}$}%
\KY{$\tan$}%
\KY{$\tan^{-1}$}\\

\KY{$($}%
\KY{$)$}%
\KN{$\times$}%
\KN{$\div$}%
\KN{$+$}%
\KN{$-$}%
\KN{$=$}\\

\KN{$1$}%
\KN{$2$}%
\KN{$3$}%
\KN{$4$}%
\KN{$5$}%
\KN{$6$}%
\KN{$7$}%
\KN{$8$}%
\KN{$9$}%
\KN{$0$}\\

\KN{1}%
\KN{2}%
\KN{3}%
\KN{4}%
\KN{5}%
\KN{6}%
\KN{7}%
\KN{8}%
\KN{9}%
\KN{0}\\

\end{document}
1
  • Thank you, this actually looks slightly better in my larger document than the previous answer. I've implemented the $ symbols in both of my definitions, in my MWE I was still testing which looked better. Commented Feb 4, 2016 at 17:03
6

I have solved the first two by using:

\let\KYo\KY
\let\KNo\KN
\renewcommand{\KY}[1]{\KYo{\makebox[1em][c]{\strut #1}}}
\renewcommand{\KN}[1]{\KNo{\makebox[1em][c]{\strut #1}}}

Just before the \begin{document} (to show the idea in a clear way and to avoid repeating all the code here...).

The idea is using a fixed width box and setting the height with a \strut. (You can have it higher using something like \vrule width 0pt height 3ex depth 1ex \relax instead of the \strut). Like this:

Output ith \strut

Or with the rule:

Output with 3ex height and 1ex depth rule

For the inverse thing, I would use the same trick using the stack engine package, and a rule in front to set the height.

(BTW I had to remove line 113 of you MWE because it didn't compile (extra \else or something like that). Also it needs the most modern versions of a lot of packages).

4
  • I will have to look into the stack engine package, but your answer solves my first two problems very well, thank you. About line 113: I have no idea what's going on, apparently the LCD package has difficulty compiling what it calls "multi-letter characters" if they appear at the end of their display syntax, such as my user defined {sc} character. I solved this after several attempts by putting a space after such a character, and it compiles for me but I have no idea why it works, or why it doesn't work for you. Commented Jan 31, 2016 at 15:58
  • Another last minute question, what did you mean by "Also it needs the most modern versions of a lot of packages"? Commented Jan 31, 2016 at 16:06
  • I have a stock LaTeX from Ubuntu 14.04 and I had to download the newest tcolorbox and lcd package to compile it... :-) --- no problem, really. My LaTeX is a bit outdated.
    – Rmano
    Commented Jan 31, 2016 at 16:32
  • 2
    It must be said that this looks awesome.
    – McGafter
    Commented Feb 2, 2016 at 8:53

You must log in to answer this question.

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