Skip to content

feat(convert): support color-related optional arguments for commands#213

Merged
OrangeX4 merged 3 commits into
mitex-rs:mainfrom
Shichien:fix/textcolor
Apr 4, 2026
Merged

feat(convert): support color-related optional arguments for commands#213
OrangeX4 merged 3 commits into
mitex-rs:mainfrom
Shichien:fix/textcolor

Conversation

@Shichien

Copy link
Copy Markdown
Collaborator

Fixes: #212, #206. This PR refactors the parsing and code generation logic for color-related commands in converter.rs and updates the corresponding definitions in standard.typ. It now provides full support for optional arguments such as [rgb], [RGB], [HTML], [cmyk], and [gray] color models for \color, \textcolor, and \colorbox.

For #206

#mi(`
\begin{tabular}{rrr}
  \toprule
  \textbf{Time} & \textbf{Velocity} & \textbf{Distance} \\
  \midrule
  5.0 & 5.0 & \color{blue}{12.5}\\
  20.0 & \textcolor{red}{20.0} & \textcolor{blue}{200.0} \\
  \bottomrule
\end{tabular}`,
)
Before After
before after

For #212

#import "@preview/mitex:0.2.6": *
#mitex(`\textbf{\textcolor{red}{A}}`)
#mitex(`\textcolor{red}{\textbf{A}}`)
Before After
Image image

Features

For \color command

// Basic color name switch
#mitext(`{{\color{red} Red text } followed by normal text.}`)

// Different color models
#mitext(`{\color[rgb]{0.1, 0.7, 0.3} Green RGB text.}`)
#mitext(`{\color[HTML]{FF4500} Orange HTML text.}`)
#mitext(`{\color[cmyk]{1,0,1,0} Bright Green CMYK text.}`)
#mitext(`{\color[gray]{0.4} Dark Grey text.}`)

// Global color persistence across text and math
#mitext(`\color{magenta} Text and math $\alpha + \beta$ are both magenta.`)

// Nested color stack test
#mitext(`{\color{red} Red text {\color{blue} Blue text} back to Red.}`)

// Switching color inside math mode
#mitext(`$ x + {\color{red} y^2} + z $`)

// Consecutive declarations
#mitext(`\color{red} \color{blue} \color{green} Only Green should be visible.`)

// Edge case: Empty scope
#mitext(`{\color{red}}`)
After (Current PR) LaTeX
image image

For \textcolor command

  • Note: As you can see, Mixed-mode color specifications (e.g., blue!50!green) are currently not supported. I plan to implement this in a future update.
#mitext(`
\textcolor{red}{Standard Red} \\
\textcolor[rgb]{0.1, 0.5, 0.8}{RGB Float Vector} \\
\textcolor[RGB]{20, 120, 200}{RGB Integer Vector} \\
\textcolor[HTML]{FF5733}{HTML Hex Caps} \\
\textcolor[HTML]{aacc00}{HTML Hex Lower} \\
\textcolor{blue!50!green}{Mixed Color Name (Fallback to map lookup)}
`)

#mitex(`
\textcolor{red}{
  \mathbf{
    \int_{
      \textcolor[rgb]{0,0,1}{
        \text{start \textcolor{orange}{$\infty$}}
      }
    }^{
      \textcolor{purple}{ \text{\textbf{end}} }
    }
    \textcolor[HTML]{008800}{x^2} \, dx
  }
} = \textcolor{cyan}{\mathfrak{Result}}
`)

#mitext(`
Normal
\textcolor [rgb] { 1, 0, 0 } { Spaced Args }
\textcolor
  [HTML]
  {0000FF}
  { Multi-line Args }
\textbf{\textcolor{red}{BoldRed}} vs \textcolor{red}{\textbf{RedBold}}
`)

#mitex(`
\sum_{\textcolor{red}{i}=0}^{\textcolor{blue}{\infty}} \frac{\textcolor[rgb]{0.9,0.1,0.1}{\sqrt{x^2+y^2}}}{\textcolor{green}{\overline{z}}}
+ \sqrt[\textcolor{purple}{3}]{\textcolor{orange}{8}}
`)
After (Current PR) LaTeX
image image

For \colorbox command

  • Change \colorbox command align with the latex default value, see: Colored-boxes.
  • Support optional arguments.
  • Note: colorbox renders the boundary even if body is empty, which is consistent with the behavior of LaTeX.
  • Note: In mitex v0.2.6, this only renders correctly inside #mitex().
// Base colorbox test
#mitext(`\colorbox{red}{Red Box}`)

// Different color models
#mitext(`\colorbox[rgb]{0.2, 0.8, 0.2}{Green RGB Box}`) // Error(mitex v0.2.6): Unknown symbol modifier

#mitext(`\colorbox[HTML]{445566}{Magenta HTML Box}`) // Error(mitex v0.2.6): Unknown symbol modifier

#mitext(`\colorbox[cmyk]{0,1,1,0}{Cyan Magenta Yellow Black Box}`) // Error(mitex v0.2.6): Unknown symbol modifier

#mitext(`\colorbox[gray]{0.5}{Grey Box}`) // Error(mitex v0.2.6): Unknown symbol modifier

// Nested colorboxes
#mitext(`\colorbox{black}{\color{white}White text in Black Box}`)

#mitext(`\colorbox{yellow}{\textcolor{red}{Red text in Yellow Box}}`)

// Math mode inside colorbox
#mitext(`\colorbox{cyan}{$E=mc^2$}`)

// Text mode inside colorbox
#mitext(`\colorbox{cyan}{\text{Text in Cyan Box}}`)

// Empty colorbox
#mitext(`\colorbox{red}{}`)
Before (mitex v0.2.6) After (Current PR) LaTeX
image image image

Warning

To preserve some of Typst's aesthetic, I set radius: 2pt, if this is deemed inconsistent with mitex's status as a LaTeX converter, please remove it.

@OrangeX4 OrangeX4 merged commit 18ef913 into mitex-rs:main Apr 4, 2026
4 checks passed
@OrangeX4

OrangeX4 commented Apr 4, 2026

Copy link
Copy Markdown
Member

Sorry, I haven't had time to deal with mitex-related PRs before, thank you for your contribution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

textbf and textcolor cannot be used nested

2 participants