From: ram@zedat.fu-berlin.de   
      
   Anthk NM wrote or quoted:   
   >There's the aamath package for Unix where you can display formulae   
   >as ASCII ART:   
   >https://github.com/gchudnov/aamath   
   >Check the documentation; it has an option to shrink the radicals.   
      
    Ah, thanks for the info!   
      
    I'm actually planning to write something like that myself.   
      
    As a quick little trial run, I recently "implemented square roots".   
      
    You can see three sample outputs here:   
      
    .-   
   \|x   
      
    .---   
   \|x+y   
      
    .-   
    |x   
    |-   
   \|y   
      
    A rectangular area made of characters is described by a "box":   
      
   class Box:   
    __slots__=["content","width","origin","baseline"]   
    def __init__( self, content, width, baseline ):   
    self.content=content   
    self.width=width   
    self.baseline=baseline   
    return   
    def __str__( self ):   
    return "\n".join(self.content)   
      
    . The root class only has one method, "render_ascii_display",   
    which generates a square root sign:   
      
   class Sqrt:   
    def render_ascii_display( self, operand ):   
      
    # add bar above   
    content=["-" * operand_box.width]+operand_box.content   
      
    new_content = []   
      
    # add bar left   
    for i, line in enumerate( content ):   
    if i == 0:   
    prefix = " ."   
    elif i == len( content )-1:   
    prefix = "\|"   
    else:   
    prefix = " |"   
    new_content.append( prefix + line )   
      
    content = new_content   
      
    bar_box = Box(content, 2+operand_box.width,operand_box.baseline)   
      
    return bar_box   
      
    . Basically, it draws a line over the operand and another   
    one to its left, with the top and bottom edges ending in   
    " ." and "\|" to form a square root sign.   
      
    Since the classes for variables, sums, and quotients aren't   
    written yet, those operands are manually formatted in the   
    main program for now:   
      
   sqrt = Sqrt()   
   operand_box = Box( ["x"], 1, 0 )   
   print(sqrt.render_ascii_display(operand_box))   
   print()   
   operand_box = Box( ["x+y"], 3, 0 )   
   print(sqrt.render_ascii_display(operand_box))   
   print()   
   operand_box = Box( ["x","-","y"], 1, 1 )   
   print(sqrt.render_ascii_display(operand_box))   
   print()   
      
    . The output was shown above near the top of this post.   
      
   --- SoupGate-DOS v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|