8

I am using QuickLatex plugin and it works great. But when I tried to cite some references using \cite{paper1}, it did not seem work.

[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}

Any help?

3
  • It looks like the QuickLatex doesn't support it, maybe this is something that you could use: wordpress.org/extend/plugins/papercite
    – birgire
    Commented May 16, 2013 at 13:12
  • Is there any other solution? Using papercite, I will have to upload a seperate bibtex file to the server. If I need to use \cite{} in every my post, this approach is not very efficient.
    – fqx
    Commented May 17, 2013 at 8:47
  • This is not supported by QuickLaTeX, yet, see holoborodko.com/pavel/quicklatex/#comment-10271
    – cgnieder
    Commented Nov 9, 2015 at 10:53

1 Answer 1

1

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}
1
  • Also please note that I had to triple-escape the slashes for some reason, e.g. \\\cite. I'm not sure why, but my PHP wouldn't parse the \cite or \bibitem items otherwise. YMMV
    – phatskat
    Commented Aug 31, 2018 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.