除了使用注释中提到的其他插件之外,您还需要自己通过过滤来解析LaTeXthe_content
.
下面是一个非常粗略的示例,说明如何捕获和转换帖子内容以解析书目。请注意,我不知道LaTeX,这只是字符串解析。此外,我还在数组中跟踪引用和参考$citations
和$citation_refs
以防这对你有用,但可能不是。
#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 );
取消注释顶部的行(
#add_filter...
) 并删除最后一行
echo ...
将此用作上的筛选器
the_content
, 但是上面的代码可以在一个独立的PHP文件中运行,以了解它是如何工作的。上述输出为:
[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}