Outside of using other plugins, as mentioned in the comments, you would need to essentially parse the LaTeX yourself by filtering the_content
.
Here is a very rough example of how you might capture and transform the post content to parse a bibliography. Please note that I don’t know LaTeX, and this is just string parsing. Also, I’m tracking citations and references in the arrays $citations
and $citation_refs
in case that is useful to you, but it may not be.
#add_filter( 'the_content', 'my_latex_citation_filter' );
function my_latex_citation_filter( $content ) {
$lines = explode( "\n", $content );
$citation_refs = [];
$citations = [];
$next_is_ref = false;
$next_ref="";
$in_bib = false;
foreach ( $lines as $index => $line ) {
preg_match( '/\\\cite\{([^}]+)\}/', $line, $matches );
if ( ! empty( $matches[1] ) ) {
$citation_refs[] = $matches[1];
$next_ref = "citation-{$matches[1]}";
$lines[ $index ] = str_replace( $matches[0], sprintf( '<a href="#%1$s">%2$s</a>', $next_ref, $matches[1] ), $line );
}
if ( $next_is_ref && $next_ref ) {
$lines[ $index ] = sprintf( '<span id="%s">' . $line . '</span>', $next_ref );
$next_ref="";
$next_is_ref = false;
}
preg_match( '/\\\bibitem\{([^}]+)\}/', $line, $matches );
if ( ! empty( $matches[1] ) ) {
$citations[] = $matches[1];
$next_is_ref = true;
unset( $lines[ $index ] );
}
}
return implode( "\n", $lines );
}
$content="
[latexpage]
Here is a citation \cite{example}.
\begin{thebibliography}
\bibitem{example}
Robert C. Merton, On the Pricing of Corporate Debt: The Risk Structure of Interest Rates. \textit{Journal of Finance} 1974; \textbf{2}:449–470.
\end{thebibliography}
";
echo my_latex_citation_filter( $content );
Uncomment the line at the top (#add_filter...
) and remove the last line echo ...
to use this as a filter on the_content
, but the code as-is above can be run inside of a standalone PHP file to see how it works. The output of the above is:
[latexpage]
Here is a citation <a href="#citation-example">example</a>.
\begin{thebibliography}
<span id="citation-example">Robert C. Merton, On the Pricing of Corporate Debt: The Risk Structure of Interest Rates. \textit{Journal of Finance} 1974; \textbf{2}:449–470.</span>
\end{thebibliography}