我想用 latex 模拟计算器的输入和显示,在查看了几个像tipfr
和这样的软件包ticollege
并发现它们不能满足我的需求后,我决定自己用tcolorbox
,并从这里拼凑出一些答案。这是一个 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}
它的外观和工作方式几乎完全符合我的需要,但是我遇到了一些困难:
1)按键不都在同一行
2)不同按键内的文本不在同一行
3) 我不知道如何在按键上添加第二个功能,比如在按键sin^{-1}
上sin
,同时解决前两个问题。
任何帮助,将不胜感激。
答案1
我可以对 Rmano 的答案进行一些修改。\strut
或类似的东西可以直接放入 的定义中\KY
,并\KN
使用before upper
选项,例如
before upper=\vrule width 0pt height 2ex depth 1ex\relax,
我注意到你\KY{$...$}
在大多数情况下使用。$
符号也可以放入\KY
和的定义中\KN
。我复制了一份,\KY
如下\KYM
所示
\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=$,
}
现在,应用程序是\KYm{...}
在数学模式下设置一些东西。
完整代码如下:
\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}
答案2
我已经通过使用以下方法解决了前两个问题:
\let\KYo\KY
\let\KNo\KN
\renewcommand{\KY}[1]{\KYo{\makebox[1em][c]{\strut #1}}}
\renewcommand{\KN}[1]{\KNo{\makebox[1em][c]{\strut #1}}}
就在之前\begin{document}
(以清晰的方式展示想法并避免在这里重复所有的代码......)。
这个想法是使用一个固定宽度的框,并用 设置高度\strut
。(您可以使用类似的东西\vrule width 0pt height 3ex depth 1ex \relax
代替 来使其更高\strut
)。像这样:
或者按照以下规则:
对于相反的情况,我会使用同样的技巧堆栈引擎包,并在前面设置一条规则来设置高度。
(顺便说一句,我不得不删除 MWE 的第 113 行,因为它无法编译(额外\else
或类似的东西)。此外,它需要许多软件包的最新版本)。