如何用「if」-「then」-「else」寫 LaTex 指令?

如何用「if」-「then」-「else」寫 LaTex 指令?

我讀LaTeX \if 條件演算法中的if和then怎麼寫?,但到目前為止它還沒有給我敲響警鐘。

我正在嘗試創建一個名為點技能像下面這樣。背後的邏輯是:

  • 接收兩個參數
  • 將計數器 x 從 1 迭代到 #2
  • 如果我的計數器填充> 0,我畫一個完整的點並減少計數器
  • 否則我畫一個空點
\newcommand{\dottedSkill}[2]{
    %parameter 1 the filled dots
    %parameter 2 the max dots

    \newcounter{x}
    \newcounter{filled}
    \setcounter{filled}{#1}
    \forloop{x}{1}{\value{x} <= #2}
    {
        \ifnum \filled<1
            \Circle
        \else
            \CIRCLE
            \addtocounter{filled}{-1}
        \fi
    }
}%

我不確定問題是否出在命令定義或其他地方,但是當我在文件上調用它時,我得到:

  • 未定義的控制序列。 \dottedSkill{2}{9}。
  • 缺失數字,視為零。 \dottedSkill{2}{9}
  • 未定義的控制序列。 \dottedSkill{2}{9}。
  • 缺失數字,視為零。 \dottedSkill{2}{9}

我的函數寫得正確嗎?

答案1

您的設定當前的問題是您正在定義filled計數器每一個你跑步的時間\dottedSkill。您應該將這些定義從您的命令中刪除\dottedSkill

另外,您應該只繪製實心圓圈,然後繪製空心圓圈:

在此輸入影像描述

\documentclass{article}

\usepackage{multido}

\newcommand{\Circle}{\textbullet}
\newcommand{\CIRCLE}{$\circ$}

\newcommand{\dottedSkill}[2]{%
  %parameter 1 the filled dots
  %parameter 2 the max dots  
  \multido{\ix=1+1}{#1}{\Circle}%
  \ifnum#1<#2 \multido{\ix=#1+1}{\numexpr#2-#1}{\CIRCLE}\fi
}%

\begin{document}

\dottedSkill{2}{9}

\dottedSkill{0}{9}

\dottedSkill{10}{9}

\dottedSkill{5}{9}

\end{document}

multido作品類似forloop迭代某個值。

答案2

\documentclass{article}
\usepackage{forloop}
\newcounter{x}
\newcounter{filled}
\newcommand{\dottedSkill}[2]{%
    %parameter 1 the filled dots
    %parameter 2 the max dots
    \setcounter{filled}{#1}%
    \forloop{x}{0}{\value{x} < #2}%
    {%
        \ifnum \thefilled < 1 
            $\circ$%\Circle
        \else 
            $\bullet$%\CIRCLE 
            \addtocounter{filled}{-1}%
        \fi
    }%
}%
\begin{document}
\dottedSkill{2}{9}
\end{document}

在此輸入影像描述

答案3

我的第一個問題是您是否真的需要條件。僅在 XY 問題的情況下(真正的問題不是使循環工作,而是使用帶有兩個參數的巨集進行評分),這是一種更簡單的方法:

姆韋

\documentclass{article}
\newcommand\score[2][10]{
\makebox[#2em]{\cleaders\hbox to 1em{\Large\hss$\bullet$\hss}\hfill}%
\makebox[\dimexpr#1em-#2em]{\cleaders\hbox to 1em{\Large\hss$\circ$\hss}\hfill}\par} 
\begin{document}
\score[5]{0} 
\score[5]{1}
\score[5]{3}
\score[5]{5}
\score{0} 
\score{3} 
\score{6}
\score{10}
\end{document}

答案4

感謝你們所有人,我能夠調整我的解決方案。

我將此連結用作圓圈類型的參考:https://aneescraftsmanship.com/circle-symbols%E2%97%8B%E2%97%8F%E2%97%8D%E2%97%97%E2%97%94%E2%97%99%E2% A6%BF-乳膠/

\usepackage{wasysym}

\newcommand{\EmptyDot}{\Circle}
\newcommand{\FilledDot}{$\CIRCLE$}

\newcommand{\dottedSkill}[2]{%
    %parameter 1 the filled dots
    %parameter 2 the max dots  
    \multido{\ix=1+1}{#1}{\FilledDot}%
    \ifnum#1<#2 \multido{\ix=#1+1}{\numexpr#2-#1}{\EmptyDot}\fi
}%

相關內容