michal-h21 / rdfref

Cross-referencing system for LaTeX, inspired with RDF
16 stars 2 forks source link

Dependency graph of equations? #9

Open ouboub opened 2 days ago

ouboub commented 2 days ago

Hi

would it be possible to support equations in the dependency graph. Here are two images, both generated, manually, with quiver

equation12

michal-h21 commented 2 days ago

This could be a start:

\documentclass{article}
\usepackage{amsmath}
\numberwithin{equation}{section}
\usepackage{rdfref}
\begin{document}

% we need to declare some properties for the eq: prefix 
\AddRdfType{eq}{
  \AddPropertyEx{rdf:type}{eq:equation}
  \AddPropertyEx{doc:pageNo}{\thepage}
  % this will be used in the graph label, change to your liking
  \AddPropertyEx{rdfs:label}{Equation \theequation}
}

\section{first section}
\rdflabel{sec:first}
\begin{equation}
  a = b + c
  % each label for equation must use the eq: prefix
  \rdflabel{eq:hello}
\end{equation}

This equation links to equation~\rdfref{eq:second}, as well as to equation~\rdfref{eq:third}.

\section{another}
\rdflabel{sec:another}

\begin{equation}
  a = d + c 
  \rdflabel{eq:second}
\end{equation}

Link to another equation~\rdfref{eq:third}

\begin{equation}
  d = x + y
  \rdflabel{eq:third}
\end{equation}

\newwrite\graphwrite
\immediate\openout\graphwrite=\jobname.dot

\makeatletter
\newcommand\graphout[1]{\protected@write\graphwrite{}{#1}}

\graphout{digraph hello\@charlb} % save graph header

% loop over all equation object
\Bind{?curreq}{rdf:type}{eq:equation}{%
  % find all \rdfref objects that points to the equation
  \Bind{?curreq}{doc:referedBy}{?blank}{%
    % now we need to find the parent object of \rdfref
    \Bind{?blank}{doc:hasParent}{?parent}{
      % this will filter only equations
      \Bind{?parent}{rdf:type}{eq:equation}{%
        \noindent Equation \GetValProperty{?curreq}{rdfs:label} is referenced by \GetValProperty{?parent}{rdfs:label}\par%
        \graphout{"\GetValProperty{?parent}{rdfs:label}" -> "\GetValProperty{?curreq}{rdfs:label}"}

      }
    }
  }%
}

\graphout{\@charrb} % save footer
\closeout\graphwrite
\makeatother

\end{document}

You need to update rdfref, because I made a change that save the current object outside environments.

This is the result:

image

It saves the DOT file to \jobname.dot:

digraph hello{
"Equation 1.1" -> "Equation 2.1"
"Equation 1.1" -> "Equation 2.2"
"Equation 2.1" -> "Equation 2.2"
}

image

ouboub commented 1 day ago

"MH" == Michal Hoftich @.***> writes:

This could be a start:

\documentclass{article}
\usepackage{amsmath}
\numberwithin{equation}{section}
\usepackage{rdfref}
\begin{document}

% we need to declare some properties for the eq: prefix 
\AddRdfType{eq}{
  \AddPropertyEx{rdf:type}{eq:equation}
  \AddPropertyEx{doc:pageNo}{\thepage}
  % this will be used in the graph label, change to your liking
  \AddPropertyEx{rdfs:label}{Equation \theequation}
}

\section{first section}
\rdflabel{sec:first}
\begin{equation}
  a = b + c
  % each label for equation must use the eq: prefix
  \rdflabel{eq:hello}
\end{equation}

This equation links to equation~\rdfref{eq:second}, as well as to equation~\rdfref{eq:third}.

\section{another}
\rdflabel{sec:another}

\begin{equation}
  a = d + c 
  \rdflabel{eq:second}
\end{equation}

Link to another equation~\rdfref{eq:third}

\begin{equation}
  d = x + y
  \rdflabel{eq:third}
\end{equation}

\newwrite\graphwrite
\immediate\openout\graphwrite=\jobname.dot

\makeatletter
***@***.***\graphwrite{}{#1}}

\graphout{digraph ***@***.***} % save graph header

% loop over all equation object
\Bind{?curreq}{rdf:type}{eq:equation}{%
  % find all \rdfref objects that points to the equation
  \Bind{?curreq}{doc:referedBy}{?blank}{%
    % now we need to find the parent object of \rdfref
    \Bind{?blank}{doc:hasParent}{?parent}{
      % this will filter only equations
      \Bind{?parent}{rdf:type}{eq:equation}{%
        \noindent Equation \GetValProperty{?curreq}{rdfs:label} is referenced by \GetValProperty{?parent}{rdfs:label}\par%
        \graphout{"\GetValProperty{?parent}{rdfs:label}" -> "\GetValProperty{?curreq}{rdfs:label}"}

      }
    }
  }%
}

***@***.***} % save footer
\closeout\graphwrite
\makeatother

\end{document}

You need to update rdfref, because I made a change that save the current object outside environments.

This is the result:

Thanks very much

It saves the DOT file to \jobname.dot:

digraph hello{
"Equation 1.1" -> "Equation 2.1"
"Equation 1.1" -> "Equation 2.2"
"Equation 2.1" -> "Equation 2.2"
}

But if I insert math in that dot file manually like digraph hello{ "$\int f dx$" -> "Equation 2.1" "Equation 1.1" -> "Equation 2.2" "Equation 2.1" -> "Equation 2.2" }

It will be displayed literally, but that I presume is a limitation of the dot format, right?

Thanks

michal-h21 commented 1 day ago

You can convert dot to LaTeX using dot2tex, this variant should keep LaTeX math:

 $ dot2tex --figonly sample.dot  -t raw filename.tex > graph.tex

The problem is how to save contents of equations. This version defines new environment rdfequation, which can do that. The label is passed as an optional argument:

\documentclass{article}
\usepackage{amsmath}
\numberwithin{equation}{section}
\usepackage{tikz}
% \usepgflibrary{path}
% \usepgflibrary{graphdrawing}
\usepackage{rdfref}
\NewDocumentEnvironment{rdfequation}{o +b}{
  \begin{equation}
    #2 
    \IfBlankF{#1}{
      \rdflabel{#1}
      \AssignProperty{#1}{eq:value}{$$#2$$}
    }
  \end{equation}
}{}
\begin{document}

% we need to declare some properties for the eq: prefix 
\AddRdfType{eq}{
  \AddPropertyEx{rdf:type}{eq:equation}
  \AddPropertyEx{doc:pageNo}{\thepage}
  % this will be used in the graph label, change to your liking
  \AddPropertyEx{rdfs:label}{Equation \theequation}
}

\section{first section}
\rdflabel{sec:first}
\begin{rdfequation}[eq:hello]
  a = b + c
  % each label for equation must use the eq: prefix
  % \rdflabel{eq:hello}
\end{rdfequation}

This equation links to equation~\rdfref{eq:second}, as well as to equation~\rdfref{eq:third}.

\section{another}
\rdflabel{sec:another}

\begin{rdfequation}[eq:second]
  a = d + c 
  % \rdflabel{eq:second}
\end{rdfequation}

Link to another equation~\rdfref{eq:third}

\begin{rdfequation}[eq:third]
  d = x + y
  % \rdflabel{eq:third}
\end{rdfequation}

\newwrite\graphwrite
\immediate\openout\graphwrite=\jobname.dot

\makeatletter
\newcommand\graphout[1]{\protected@write\graphwrite{}{#1}}

% \AssignProperty{eq:third}{rdfs:label}{\detokenized{$a=b^2$}}

\graphout{digraph hello\@charlb} % save graph header

% loop over all equation object
\Bind{?curreq}{rdf:type}{eq:equation}{%
  % find all \rdfref objects that points to the equation
  \Bind{?curreq}{doc:referedBy}{?blank}{%
    % now we need to find the parent object of \rdfref
    \Bind{?blank}{doc:hasParent}{?parent}{
      % this will filter only equations
      \Bind{?parent}{rdf:type}{eq:equation}{%
        \noindent Equation \GetValProperty{?curreq}{rdfs:label} is referenced by \GetValProperty{?parent}{rdfs:label}\par%
        \graphout{"\GetValProperty{?parent}{eq:value}" -> "\GetValProperty{?curreq}{eq:value}"}
      }
    }
  }%
}

\graphout{\@charrb} % save footer
\closeout\graphwrite

\makeatother

\InputIfFileExists{graph.tex}{}

\end{document}

image