不确定如何措辞这个问题的标题。
基本上,我正在尝试创建一个允许在文档中使用五进制系统的包。
该项目可以查看在背页上。
我已经让它能够处理 20 以下的数字,但是之后它就变得有点复杂了。
我有一些我想做的伪代码,但就是无法弄清楚如何在乳胶中做到这一点:
\newcommand{\pent}[1]
{
% Draw numbers 1-20 (working)
}
\newcommand{\pentimal}[1]
{
\ifthenelse{#1<21}
{
\pent{#1}
}
{
\while(#1>20)
{
\pent{20}
#1 -= 20
}
\pent{#1}
}
}
到目前为止我已经得到了这个:
\newcommand{\pentimal}[1]{%
\ifthenelse{#1<21}%
{\pent{#1}}%
{%
\pgfmathparse{#1 + 0}%
\whiledo{\pgfmathresult{}>20}%
{%
\pent{20}%
\pgfmathparse{\pgfmathresult{} - 20}%
}%
\pent{\pgfmathresult{}}%
}%
}
但我不断收到没有丝毫帮助的错误(例如! Missing = inserted for \ifnum.
尽管没有使用\ifnum
)。
答案1
你可以使用expl3
:
\documentclass{article}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
\ExplSyntaxOn
\cs_new:Nn \legend_pentimal_digit:n
{
\int_case:nn { #1 }
{
{1}{a}
{2}{b}
{3}{c}
{4}{d}
{5}{e}
{6}{f}
{7}{g}
{8}{h}
{9}{i}
{10}{j}
{11}{k}
{12}{l}
{13}{m}
{14}{n}
{15}{o}
{16}{p}
{17}{q}
{18}{r}
{19}{s}
{20}{t}
}
}
\cs_new:Nn \legend_pentimal:n
{
\int_compare:nTF { 0 < #1 <= 20 }
{
\legend_pentimal_digit:n { #1 }
}
{
\legend_pentimal_digit:n { 20 }
\legend_pentimal:e { \int_eval:n { #1 - 20 } }
}
}
\cs_generate_variant:Nn \legend_pentimal:n { e }
\NewExpandableDocumentCommand{\pent}{m}
{
\legend_pentimal:n { #1 }
}
\ExplSyntaxOff
\begin{document}
\pent{1}
\pent{2}
\pent{3}
\pent{4}
\pent{5}
\pent{6}
\pent{7}
\pent{8}
\pent{9}
\pent{10}
\pent{11}
\pent{12}
\pent{13}
\pent{14}
\pent{15}
\pent{16}
\pent{17}
\pent{18}
\pent{19}
\pent{20}
\pent{345}
\end{document}
您将使用代码来表示数字,而不是 定义中的字母\legend_pentimal_digit:n
。请记住使用
\NewDocumentCommand{\pentimelOne}{}{<code>}
为数字 1 等等。
代码只是在最后余数之前重复了 20 的数字,就像您在代码中所做的那样。如果数字大于 20,则该函数会打印数字 20,然后对小于 20 的数字调用自身。
从您的项目中获取代码:
\documentclass{article}
%\usepackage{xparse} % not needed for LaTeX 2020-10-01 or later
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usetikzlibrary{positioning}
\usepackage{ifthen}
\usepackage{pgfmath}
\NewDocumentCommand{\pent}{m}{%
\begin{tikzpicture}[x=1pt,y=1pt,yscale=-0.25,xscale=0.25]
\tikzset{line width=2pt}
\ifthenelse{#1<11}{\clip rectangle (10pt, 25pt);}{\clip rectangle (20pt, 25pt);}
% Vertical line
\draw (10,0) -- (10,25) ;
% Left line 1
\ifthenelse{\(#1<10\) \AND \(\NOT #1=5\)}{\draw (0,0) -- (10,0) ;}{}
% Left line 2
\ifthenelse{\(#1>1 \AND #1<5\) \OR \(#1>6 \AND #1<10\)}{\draw (0,5) -- (10,5) ;}{}
% Left line 3
\ifthenelse{\(#1>2 \AND #1<5\) \OR \(#1>7 \AND #1<10\)}{\draw (0,10) -- (10,10) ;}{}
% Left line 4
\ifthenelse{#1=4 \OR #1=9}{\draw (0,15) -- (10,15) ;}{}
% Right line 1
\ifthenelse{\(#1>10\) \AND \(\NOT #1=15\) \AND \(#1<20\)}{\draw (10,0) -- (20,0) ;}{}
% Right line 2
\ifthenelse{\(#1>11 \AND #1<15\) \OR \(#1>16 \AND #1<20\)}{\draw (10,5) -- (20,5) ;}{}
% Right line 3
\ifthenelse{\(#1>12 \AND #1<15\) \OR \(#1>17 \AND #1<20\)}{\draw (10,10) -- (20,10) ;}{}
% Right line 4
\ifthenelse{#1=14 \OR #1=19}{\draw (10,15) -- (20,15) ;}{}
% Bottom-left loop
\ifthenelse{#1>4}{\draw (10,25) .. controls (9.66,25.02) and (9.31,25.03) .. (8.95,25.03) .. controls (4.85,25.03) and (1.53,23.72) .. (1.53,22.11) .. controls (1.53,20.5) and (4.85,19.2) .. (8.95,19.2) .. controls (9.31,19.2) and (9.66,19.21) .. (10,19.23) -- cycle ;}{}
% Top-left loop
\ifthenelse{#1>9}{\draw (10,5.77) .. controls (9.66,5.79) and (9.31,5.8) .. (8.95,5.8) .. controls (4.85,5.8) and (1.53,4.5) .. (1.53,2.89) .. controls (1.53,1.28) and (4.85,-0.03) .. (8.95,-0.03) .. controls (9.31,-0.03) and (9.66,-0.02) .. (10,0) -- cycle ;}{}
% Bottom-right loop
\ifthenelse{#1>14}{\draw (10,19.23) .. controls (10.34,19.21) and (10.69,19.2) .. (11.05,19.2) .. controls (15.15,19.2) and (18.47,20.5) .. (18.47,22.11) .. controls (18.47,23.72) and (15.15,25.03) .. (11.05,25.03) .. controls (10.69,25.03) and (10.34,25.02) .. (10,25) -- cycle ;}{}
% Top-right loop
\ifthenelse{#1>19}{\draw (10,0) .. controls (10.34,-0.02) and (10.69,-0.03) .. (11.05,-0.03) .. controls (15.15,-0.03) and (18.47,1.28) .. (18.47,2.89) .. controls (18.47,4.5) and (15.15,5.8) .. (11.05,5.8) .. controls (10.69,5.8) and (10.34,5.79) .. (10,5.77) -- cycle ;}{}
\end{tikzpicture}
}
\ExplSyntaxOn
\cs_new:Nn \legend_pentimal_digit:n
{
\pent{ #1 }
}
\cs_new:Nn \legend_pentimal:n
{
\int_compare:nTF { 0 < #1 <= 20 }
{
\legend_pentimal_digit:n { #1 }
}
{
\legend_pentimal_digit:n { 20 }
\legend_pentimal:e { \int_eval:n { #1 - 20 } }
}
}
\cs_generate_variant:Nn \legend_pentimal:n { e }
\NewExpandableDocumentCommand{\pentimal}{m}
{
\legend_pentimal:n { #1 }
}
\ExplSyntaxOff
\newcommand{\rom}[1]{\romannumeral#1\relax}
\begin{document}
\begin{tabular}{|c|c|c|}\hline
\textbf{Arabic} & \textbf{Pentimal} & \textbf{Roman} \\\hline
1 & \pentimal{1} & \rom{1} \\\hline
2 & \pentimal{2} & \rom{2} \\\hline
3 & \pentimal{3} & \rom{3} \\\hline
4 & \pentimal{4} & \rom{4} \\\hline
5 & \pentimal{5} & \rom{5} \\\hline
6 & \pentimal{6} & \rom{6} \\\hline
7 & \pentimal{7} & \rom{7} \\\hline
8 & \pentimal{8} & \rom{8} \\\hline
9 & \pentimal{9} & \rom{9} \\\hline
10 & \pentimal{10} & \rom{10} \\\hline
11 & \pentimal{11} & \rom{11} \\\hline
12 & \pentimal{12} & \rom{12} \\\hline
13 & \pentimal{13} & \rom{13} \\\hline
14 & \pentimal{14} & \rom{14} \\\hline
15 & \pentimal{15} & \rom{15} \\\hline
16 & \pentimal{16} & \rom{16} \\\hline
17 & \pentimal{17} & \rom{17} \\\hline
18 & \pentimal{18} & \rom{18} \\\hline
19 & \pentimal{19} & \rom{19} \\\hline
20 & \pentimal{20} & \rom{20} \\\hline
21 & \pentimal{21} & \rom{21} \\\hline
22 & \pentimal{22} & \rom{22} \\\hline
23 & \pentimal{23} & \rom{23} \\\hline
24 & \pentimal{24} & \rom{24} \\\hline
25 & \pentimal{25} & \rom{25} \\\hline
\end{tabular}
\begin{tabular}{|c|c|c|}\hline
\textbf{Arabic} & \textbf{Pentimal} & \textbf{Roman} \\\hline
26 & \pentimal{26} & \rom{26} \\\hline
27 & \pentimal{27} & \rom{27} \\\hline
28 & \pentimal{28} & \rom{28} \\\hline
29 & \pentimal{29} & \rom{29} \\\hline
30 & \pentimal{30} & \rom{30} \\\hline
31 & \pentimal{31} & \rom{31} \\\hline
32 & \pentimal{32} & \rom{32} \\\hline
33 & \pentimal{33} & \rom{33} \\\hline
34 & \pentimal{34} & \rom{34} \\\hline
35 & \pentimal{35} & \rom{35} \\\hline
36 & \pentimal{36} & \rom{36} \\\hline
37 & \pentimal{37} & \rom{37} \\\hline
38 & \pentimal{38} & \rom{38} \\\hline
39 & \pentimal{39} & \rom{39} \\\hline
40 & \pentimal{40} & \rom{40} \\\hline
41 & \pentimal{41} & \rom{41} \\\hline
42 & \pentimal{42} & \rom{42} \\\hline
43 & \pentimal{43} & \rom{43} \\\hline
44 & \pentimal{44} & \rom{44} \\\hline
45 & \pentimal{45} & \rom{45} \\\hline
46 & \pentimal{46} & \rom{46} \\\hline
47 & \pentimal{47} & \rom{47} \\\hline
48 & \pentimal{48} & \rom{48} \\\hline
49 & \pentimal{49} & \rom{49} \\\hline
50 & \pentimal{50} & \rom{50} \\\hline
\end{tabular}
\end{document}