jgm / pandoc

Universal markup converter
https://pandoc.org
Other
33.87k stars 3.34k forks source link

New Feature: internal links to tables and figures and headers #813

Open GeraldLoeffler opened 11 years ago

GeraldLoeffler commented 11 years ago

It's currently possible to include internal links to sections. I'd like to propose a similar feature for links to figures/images and tables.

It may make sense to provide this feature only if the figure/image or table that is being linked to has a caption. In that case Pandoc can today automatically generate a number for the figure or table and include it in the caption, e.g. "Figure 15".

At the most basic, the text of the link would be provided by the user, as is currently the case for links to sections.

Of course it would be very convenient if the automatically generated number for the figure or table would also be used for the text of the link, e.g. "as can be seen in Figure 15, blah", where "Figure 15" would be the internal link whose text is auto-generated from the figure it points to.

wenbopeng commented 10 months ago

With latest pandoc 3.1.2-1 on upcoming Debian 12 only equations are not numbered - resulting document has [eq:eq]. See below image:

pandoc3@debian12

Thanks!

Could you please provide the markdown and pandoc command of the screenshot?

N0rbert commented 10 months ago

@wenbopeng let's retest with latest Pandoc 3.1.8 on Debian 12:

cat > /tmp/simple.tex <<\EOF
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{lmodern}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math}
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\hypersetup{
  pdftitle={Simple document},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}
\urlstyle{same} % disable monospaced font for URLs
\usepackage[margin=1in]{geometry}
\usepackage{longtable,booktabs,array}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
\usepackage[normalem]{ulem}
% Avoid problems with \sout in headers with hyperref
\pdfstringdefDisableCommands{\renewcommand{\sout}{}}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\usepackage[]{biblatex}
\addbibresource{bibliography.bib}

\title{Simple document}
\author{}
\date{\vspace{-2.5em}}

\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}{Lemma}[section]
\newtheorem{corollary}{Corollary}[section]
\newtheorem{proposition}{Proposition}[section]
\newtheorem{conjecture}{Conjecture}[section]
\theoremstyle{definition}
\newtheorem{definition}{Definition}[section]
\theoremstyle{definition}
\newtheorem{example}{Example}[section]
\theoremstyle{definition}
\newtheorem{exercise}{Exercise}[section]
\theoremstyle{remark}
\newtheorem*{remark}{Remark}
\newtheorem*{solution}{Solution}
\begin{document}
\maketitle

{
\setcounter{tocdepth}{2}
\tableofcontents
}
\hypertarget{format}{%
\section{Format}\label{format}}

Read \ref{format} for details.

Or read \protect\hyperlink{format}{format}.

Formatting:

\begin{itemize}
\tightlist
\item
  sup-script or power - x\textsuperscript{2} ;
\item
  sub-script - y\textsubscript{3} ;
\item
  strike through - \sout{strike through} ;
\item
  term -- definition ;
\item
  long term --- long definition .
\end{itemize}

Here comes \textbf{footnote} \footnote{Footnote text} with its contents.

Image with caption, see \ref{fig:image}.

\begin{figure}
\centering
\includegraphics{image.png}
\caption{\label{fig:image} Image}
\end{figure}

Labeled table is below, see table \ref{tab:table}.

\begin{longtable}[]{@{}ll@{}}
\caption{\label{tab:table}\\
My table}\tabularnewline
\toprule
\textbf{Col 1 header} & \textbf{Col 2 header} \\
\midrule
\endfirsthead
\endhead
r1c1 & r1c2 \\
r2c1 & r2c2 \\
\bottomrule
\end{longtable}

Inline: \(a^2 + b^2 = c^2\).

Block:

\[ E = \frac{mc^2}{\sqrt{1-\frac{v^2}{c^2}}} \]

Labeled, see \eqref{eq:eq}:

\begin{equation}
f\left(k\right) =\binom{n}{k} p^k\left(1-p\right)^{n k}
\label{eq:eq}
\end{equation}

See LaTeX \autocite{latex} site for details.

Code is below, see \ref{exm:code}:

\begin{example}
\protect\hypertarget{exm:code}{}{\label{exm:code} }Hello world
\end{example}

\begin{verbatim}
int main(){
  return 0;
}
\end{verbatim}

\hypertarget{references}{%
\section*{References}\label{references}}
\addcontentsline{toc}{section}{References}

\printbibliography

\end{document}
EOF

cat > /tmp/bibliography.bib <<EOF
@online{latex,
title={LaTeX - A document preparation system},
url={https://www.latex-project.org}
}
EOF
cd /tmp
wget -c https://github.com/jgm/pandoc/releases/download/3.1.8/pandoc-3.1.8-1-amd64.deb
sudo apt-get update
sudo apt-get dist-upgrade -y
sudo apt-get install -y ./pandoc-3.1.8-1-amd64.deb
wget -c https://www.zotero.org/styles/ieee -O ieee.csl

cp /usr/share/plymouth/debian-logo.png image.png
pandoc /tmp/simple.tex -t docx+native_numbering --output simple.docx --table-of-contents --toc-depth 5 --number-sections --citeproc --verbose --csl ieee.csl
lowriter simple.docx

With all above code and commands I got the following rendering:

pandoc-3-1-8

So only equations are not numbered - resulting document has [eq:eq].

wenbopeng commented 10 months ago

Thank you very much. I hope to get docx from markdown. I want to insert the field code of the picture/table. The picture problem has been solved so far, but I can't find a way to number the table.

first, use -t docx+native_numbering command

![image name](img.png){#fig:img}

see`<w:fldSimple w:instr="REF ref_fig:img" />`{=openxml}...

The above syntax can be converted through lua filter

function Str(elem)
    local img_key = elem.text:match("%[@fig:(.*)%]")
    if img_key then
      return pandoc.RawInline('openxml', '<w:fldSimple w:instr="REF ref_fig:'..img_key..'" />')
    else
      return elem
    end
  end

so, you can use

![image name](img.png){#fig:img}

see [@fig:img] ...

But I don't know how to deal with the table when md2docx

N0rbert commented 10 months ago

@wenbopeng probably you have to try bookdown - see https://bookdown.org/home/about/ . It has rich functionality and uses Pandoc under the hood.

wenbopeng commented 10 months ago

@wenbopeng probably you have to try bookdown - see https://bookdown.org/home/about/ . It has rich functionality and uses Pandoc under the hood.

thankyou,I use R, but I'm not going to do that. I can already do it fine with pandoc-crossreference. But I want it to be with Field Code and I need to update the cross-reference automatically in MsWord