49 lines
1.3 KiB
TypeScript
49 lines
1.3 KiB
TypeScript
export const ACTIVE_DISTRIBUTION_COLOR = "#f97316";
|
|
|
|
function colorLatex(latex: string, color: string) {
|
|
return `\\textcolor{${color}}{${latex}}`;
|
|
}
|
|
|
|
interface FormatParenthesisExpressionLatexParams<T> {
|
|
multiplier: number;
|
|
terms: T[];
|
|
operators: ("+" | "-")[];
|
|
formatTermLatex: (term: T) => string;
|
|
activeTermIndex?: number;
|
|
activeColor?: string;
|
|
}
|
|
|
|
export function formatParenthesisExpressionLatex<T>({
|
|
multiplier,
|
|
terms,
|
|
operators,
|
|
formatTermLatex,
|
|
activeTermIndex,
|
|
activeColor = ACTIVE_DISTRIBUTION_COLOR,
|
|
}: FormatParenthesisExpressionLatexParams<T>) {
|
|
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)`;
|
|
}
|