special_tex.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from __future__ import annotations
  2. from manimlib.constants import MED_SMALL_BUFF, WHITE, GREY_C
  3. from manimlib.constants import DOWN, LEFT, RIGHT, UP
  4. from manimlib.constants import FRAME_WIDTH
  5. from manimlib.constants import MED_LARGE_BUFF, SMALL_BUFF
  6. from manimlib.mobject.geometry import Line
  7. from manimlib.mobject.types.vectorized_mobject import VGroup
  8. from manimlib.mobject.svg.tex_mobject import TexText
  9. from typing import TYPE_CHECKING
  10. if TYPE_CHECKING:
  11. from manimlib.typing import ManimColor, Vect3
  12. class BulletedList(VGroup):
  13. def __init__(
  14. self,
  15. *items: str,
  16. buff: float = MED_LARGE_BUFF,
  17. aligned_edge: Vect3 = LEFT,
  18. **kwargs
  19. ):
  20. labelled_content = [R"\item " + item for item in items]
  21. tex_string = "\n".join([
  22. R"\begin{itemize}",
  23. *labelled_content,
  24. R"\end{itemize}"
  25. ])
  26. tex_text = TexText(tex_string, isolate=labelled_content, **kwargs)
  27. lines = (tex_text.select_part(part) for part in labelled_content)
  28. super().__init__(*lines)
  29. self.arrange(DOWN, buff=buff, aligned_edge=aligned_edge)
  30. def fade_all_but(self, index: int, opacity: float = 0.25) -> None:
  31. for i, part in enumerate(self.submobjects):
  32. part.set_fill(opacity=(1.0 if i == index else opacity))
  33. class TexTextFromPresetString(TexText):
  34. tex: str = ""
  35. default_color: ManimColor = WHITE
  36. def __init__(self, **kwargs):
  37. super().__init__(
  38. self.tex,
  39. color=kwargs.pop("color", self.default_color),
  40. **kwargs
  41. )
  42. class Title(TexText):
  43. def __init__(
  44. self,
  45. *text_parts: str,
  46. font_size: int = 72,
  47. include_underline: bool = True,
  48. underline_width: float = FRAME_WIDTH - 2,
  49. # This will override underline_width
  50. match_underline_width_to_text: bool = False,
  51. underline_buff: float = SMALL_BUFF,
  52. underline_style: dict = dict(stroke_width=2, stroke_color=GREY_C),
  53. **kwargs
  54. ):
  55. super().__init__(*text_parts, font_size=font_size, **kwargs)
  56. self.to_edge(UP, buff=MED_SMALL_BUFF)
  57. if include_underline:
  58. underline = Line(LEFT, RIGHT, **underline_style)
  59. underline.next_to(self, DOWN, buff=underline_buff)
  60. if match_underline_width_to_text:
  61. underline.match_width(self)
  62. else:
  63. underline.set_width(underline_width)
  64. self.add(underline)
  65. self.underline = underline