Mimicking a calculator inputs and screen

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


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}

Tags:

Tcolorbox