def hex_to_latex(hex_color, text="Color"): # Ensure the hex code starts with '#' if hex_color.startswith('#'): hex_color = hex_color[1:] # Convert hex to RGB (0-255) r, g, b = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) # Normalize to (0-1) range for LaTeX r_norm, g_norm, b_norm = r / 255, g / 255, b / 255 # Format LaTeX command latex_command = f"{{\\textsf{{\\textcolor[rgb]{{{r_norm:.3f}, {g_norm:.3f}, {b_norm:.3f}}}{{{text}}}}}}}" return latex_command # Example usage hex_code = "#300A24" print(hex_to_latex(hex_code, "■")) # Outputs: {\textsf{\textcolor[rgb]{0.188, 0.039, 0.141}{■}}}