export const ACTIVE_DISTRIBUTION_COLOR = "#f97316"; function colorLatex(latex: string, color: string) { return `\\textcolor{${color}}{${latex}}`; } interface FormatParenthesisExpressionLatexParams { multiplier: number; terms: T[]; operators: ("+" | "-")[]; formatTermLatex: (term: T) => string; activeTermIndex?: number; activeColor?: string; } export function formatParenthesisExpressionLatex({ multiplier, terms, operators, formatTermLatex, activeTermIndex, activeColor = ACTIVE_DISTRIBUTION_COLOR, }: FormatParenthesisExpressionLatexParams) { const hasActiveTerm = activeTermIndex !== undefined; const maybeColor = (latex: string, active: boolean) => hasActiveTerm && active ? colorLatex(latex, activeColor) : latex; const termLatex = terms .map((term, index) => { const displayTerm = maybeColor( formatTermLatex(term), index === activeTermIndex, ); if (index === 0) return displayTerm; return `${operators[index - 1]} ${displayTerm}`; }) .join(" "); if (hasActiveTerm) { return `${colorLatex(`${multiplier}`, activeColor)}${colorLatex( "(", activeColor, )}${termLatex}${colorLatex(")", activeColor)}`; } return `${multiplier}\\left(${termLatex}\\right)`; }