alvinwan / TexSoup

fault-tolerant Python3 package for searching, navigating, and modifying LaTeX documents
https://texsoup.alvinwan.com
BSD 2-Clause "Simplified" License
287 stars 43 forks source link

Difference between contents, children, descendants ? #16

Closed lionpeloux closed 6 years ago

lionpeloux commented 6 years ago

Hi alvinwan,

I've been working with your TexSoup engine and it looks promising to me ! Thank you for sharing. My goal is to write a script to transform my phd thesis (huge latex) to a nice web site like mkdocs

I've wrote this little loop to visualize how to expand the tree. It can expand the Args recursively into other nodes (in case Args are also valid latex expression as in the following example).


tex = r'''
Remark that for both the circumscribed (see \cref{fig:kb_vertex_tangent_a}) and inscribed (see \cref{fig:kb_edge_tangent_a}) osculating circles~:
\begin{subequations}
\begin{alignat}{3}
    &\vect{t}_i     &&= \mathcal{R}_{\vect{x}_{i-1/2}}^{\vect{u}_{i-1}} \circ \mathcal{R}_{\vect{x}_{i}}^{\vect{t}_{i}}  (\vect{t}_i^-) \\
    &\vect{t}_i     &&= \mathcal{R}_{\vect{x}_{i}}^{\vect{t}_{i}} \circ \mathcal{R}_{\vect{x}_{i+1/2}}^{\vect{u}_{i}}  (\vect{t}_i^+)
\end{alignat}
\end{subequations}

\section{Conclusion}

This chapter has established all the geometrical tools required for our future discrete beam model. Our analysis show that for the type of structures we want to model the discrete curvature defined according to the circumscribed osculating circle is the most suitable as~:
\begin{itemize}
\item it provides an unequivocal definition of the discrete curvature in the current portion and at the extremities of the curve ;
\item it exhibits the fastest convergence when regarding the evaluation of the bending energy of typical curves ;
\item it leads to a natural local spline interpolation passing through the curve's vertices ;
\item it leads to a natural definition of the tangent vector at vertices and at midspan of edges ;
\item it enables the modeling of curvature discontinuities.
\end{itemize}
'''

toc = tex2py(tex)
soup = TexSoup(tex)

def browseContent(src, level):
    if type(src)==str:
        elp = ""
        if len(src)>100 : elp="[...]"
        print("%s str : %s %s" % ("----" * level, src.lstrip('\n').rstrip('\n').encode('utf-8')[0:100], elp))
        return
    elif type(src)==RArg:
        print("%s RArg : %s" % ("----" * level, src.value))
        return
    elif type(src)==OArg:
        print("%s OArg : %s [...]" % ("----" * level, src[0:100]))
        return
    else:
        contents = list(src.contents)
        children = list(src.children)
        print("%s NodeTex[%i,%i] : %s" % ("----" * level,len(children),len(contents),src.name))
        for content in contents:
            # print(type(content))
            browseContent(content,level+1)

browseContent(soup,0)

This example outputs:

[NodeTex(5,9)] : [tex]
---- [str] : b'Remark that for both the circumscribed (see ' 
---- [NodeTex(0,1)] : cref
-------- [str] : b'fig:kb_vertex_tangent_a' 
---- [str] : b') and inscribed (see ' 
---- [NodeTex(0,1)] : cref
-------- [str] : b'fig:kb_edge_tangent_a' 
---- [str] : b') osculating circles~:' 
---- [NodeTex(1,1)] : subequations
-------- [NodeTex(10,33)] : alignat
------------ [str] : b'\t&' 
------------ [NodeTex(0,1)] : vect
---------------- [str] : b't' 
------------ [str] : b'_i \t&&= ' 
------------ [NodeTex(0,1)] : mathcal
---------------- [str] : b'R' 
------------ [str] : b'_' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b'x' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i-1/2' 
------------ [str] : b'^' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b'u' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i-1' 
------------ [NodeTex(0,0)] : circ
------------ [NodeTex(0,1)] : mathcal
---------------- [str] : b'R' 
------------ [str] : b'_' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b'x' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i' 
------------ [str] : b'^' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b't' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i' 
------------ [str] : b'  (' 
------------ [NodeTex(0,1)] : vect
---------------- [str] : b't' 
------------ [str] : b'_i^-) \\\\\n\t&' 
------------ [NodeTex(0,1)] : vect
---------------- [str] : b't' 
------------ [str] : b'_i \t&&= ' 
------------ [NodeTex(0,1)] : mathcal
---------------- [str] : b'R' 
------------ [str] : b'_' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b'x' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i' 
------------ [str] : b'^' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b't' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i' 
------------ [NodeTex(0,0)] : circ
------------ [NodeTex(0,1)] : mathcal
---------------- [str] : b'R' 
------------ [str] : b'_' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b'x' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i+1/2' 
------------ [str] : b'^' 
------------ [RArg] : EXPANDING...
------------ [NodeTex(1,3)] : [tex]
---------------- [NodeTex(0,1)] : vect
-------------------- [str] : b'u' 
---------------- [str] : b'_' 
---------------- [RArg] : EXPANDING...
---------------- [NodeTex(0,1)] : [tex]
-------------------- [str] : b'i' 
------------ [str] : b'  (' 
------------ [NodeTex(0,1)] : vect
---------------- [str] : b't' 
------------ [str] : b'_i^+)' 
---- [NodeTex(0,1)] : section
-------- [str] : b'Conclusion' 
---- [str] : b'This chapter has established all the geometrical tools required for our future discrete beam model. ' [...]
---- [NodeTex(5,5)] : itemize
-------- [NodeTex(0,1)] : item
------------ [str] : b'it provides an unequivocal definition of the discrete curvature in the current portion and at the ex' [...]
-------- [NodeTex(0,1)] : item
------------ [str] : b'it exhibits the fastest convergence when regarding the evaluation of the bending energy of typical c' [...]
-------- [NodeTex(0,1)] : item
------------ [str] : b"it leads to a natural local spline interpolation passing through the curve's vertices ;" 
-------- [NodeTex(0,1)] : item
------------ [str] : b'it leads to a natural definition of the tangent vector at vertices and at midspan of edges ;' 
-------- [NodeTex(0,1)] : item
------------ [str] : b'it enables the modeling of curvature discontinuities.' 

Although I finally succeed in making this script run, I'm still confused about the differences between : descendants, contents, children. Would you mind adding a little explanation in the help ? It could improve the usability of this great tool for others.

Thanks ! Lionel

alvinwan commented 6 years ago

Ah definitely. Thanks for the detailed feedback, and good luck on transforming your thesis! I'll leave some notes here and resolve once I've added to the docstring:

Take the following example. Consider the \begin{itemize} environment.

\begin{itemize}
  Floating text
  \item outer text
  \begin{enumerate}
    \item nested text
  \end{enumerate}
\end{itemize}

Here are its three properties contents, children, and descendants below:

lionpeloux commented 6 years ago

Ok, thank you for these informations, its now clear for me !

Le 29 nov. 2017 à 01:25, Alvin Wan notifications@github.com a écrit :

Ah definitely. Thanks for the detailed feedback, and good luck on transforming your thesis! I'll leave some notes here and resolve once I've added to the docstring:

Take the following example. Consider the \begin{itemize} environment.

\begin{itemize} Floating text \item outer text \begin{enumerate} \item nested text \end{enumerate} \end{itemize} Here are its three properties contents, children, and descendants below:

contents: Anything and everything inside of this command. Everything needed to fully reconstruct the latex. ex. ["Floating Text", \item outer text, \begin{enumerate}]. children: Same as contents, but filter out random pieces of text. ex. Just [\item outer text, \begin{enumerate}]. descendants: all children, and all children of all children, and all children of all children of all... etc. ex. [\item outer text, \begin{enumerate}, \item nested text] — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/alvinwan/TexSoup/issues/16#issuecomment-347710593, or mute the thread https://github.com/notifications/unsubscribe-auth/AA6FSrjPWeYKe64sIjyo7OfiRyN7C7RGks5s7KRpgaJpZM4Qo1E4.