Bagaimana cara mereferensikan lingkungan mengambang seperti daftar atau gambar ke tempat mereka disebutkan?

Aug 17 2020

Saya ingin memasukkan beberapa entri kode sumber dan beberapa gambar / gambar di lampiran saya untuk lebih jelasnya lihat pertanyaan ini ), dan oleh karena itu saya akan merasa sangat berguna untuk memiliki backlink di sana (seperti yang dapat Anda miliki di bibliografi atau singkatan TOC dengan acroatau lebih), karena bagian di mana itu direferensikan sangat jauh dari tempat itu dicetak (yaitu dalam lampiran).

Singkatnya, pertanyaan ini pada dasarnya mengasumsikan "kebalikan" dari pertanyaan „Menjaga tabel / gambar dekat dengan tempat mereka disebutkan” , yaitu gambar / listing saya jauh dari tempat mereka disebutkan dan oleh karena itu saya ingin backref untuk mereka , agar orang dapat menemukan penjelasan untuk cuplikan kode atau gambar yang saya kutip ketika mereka hanya melihat lampiran.

Saya telah menggunakan hyperrefperintah untuk mereferensikan gambar atau daftar yang dibuat melalui minted, jadi misalnya \autoref, \namerefatau \fullref.

Jadi, mungkinkah entah bagaimana menambahkan referensi latar (backref / backlink) ke teks - atau mengingat ide saya yang lain untuk menggunakan judul alih-alih teks di sana di tempat lain dalam teks?

Saya misalnya ingin caption seperti ini:

Potongan kode sumber yang menunjukkan bagaimana A membuat B. (disebutkan di halaman 5, 7-8.)

Gaya yang tepat, yaitu apakah itu kompak dan melewatkan halaman atau intro apa yang digunakannya ("disebutkan pada halaman" juga bisa diganti dengan "pp." Atau singkatan lain yang mungkin sudah ditentukan dalam beberapa biblatex atau makro acro) jadi, tidak masalah, tapi saya suka gaya yang acrodigunakan.)

Sunting: Karena masih belum jelas, saya ingin sesuatu seperti yang dilakukan acro:

Saya ingin tautan balik "S. 10, 14, 27", dll. Ini ke laman. Jawaban dari @John Kormylo memberikan contoh yang bagus, tetapi belum lengkap. Tetapi saya tidak ingin mereka untuk sebuah akronim atau daftar referensi (biblatex juga bisa melakukan itu, saya tahu itu), tetapi untuk beberapa gambar / daftar / keterangan di lampiran .

BTW sudah cukup kalau backlink ke halaman , tidak harus posisinya persis.

Tidak ada MWE yang bisa saya berikan, karena tidak berfungsi disini.

Catatan: Untuk alasan yang jelas bahwa backref mungkin harus mengecualikan tautan dalam TOC seperti \listoflistingsatau \listoffigures.

Jawaban

3 moewe Aug 27 2020 at 13:24

Berikut ini adalah sesuatu yang didasarkan secara longgar pada biblatexpendekatan backref.

Dengan setiap panggilan dari \ref/ \autorefkami mengirim penanda ke .auxfile yang dirujuk labelnya pada halaman khusus ini. Pada awal setiap proses LaTeX kita dapat membaca kombinasi label-halaman dari .auxfile dan mengkompilasi untuk setiap label daftar halaman di mana ia direferensikan. Daftar ini nantinya dapat digunakan untuk pencetakan.

Dengan pendekatan sederhana ini kami hanya menautkan kembali ke halaman, bukan ke tempat persisnya label dirujuk, kami juga tidak mengeluarkan peringatan ulang LaTeX secara otomatis.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
% stolen from biblatex
% we need a way to check if printable text is in a list
\newrobustcmd{\rugk@ifprintableinlist}[2]{%
  \begingroup
    \def\rugk@tempa{\endgroup
      \@secondoftwo}%
    \renewcommand*{\do}[1]{%
      \ifstrequal{##1}{#1}
        {\def\rugk@tempa{\endgroup
           \@firstoftwo}%
         \listbreak}
        {}}%
    \dolistloop{#2}%
  \expandafter\rugk@tempa}

\newrobustcmd{\rugk@ifprintableinlistcs}[2]{%
  \expandafter\rugk@ifprintableinlistcs@i\csname #2\endcsname{#1}}
\long\def\rugk@ifprintableinlistcs@i#1#2{\rugk@ifprintableinlist{#2}{#1}}

% add <page> to the list of pages where <label> was referenced
% {<label>}{<page>}{<page int>}
\protected\def\rugk@ref@backref#1#2#3{%
  \ifcsundef{rugk@ref@backreflist@\detokenize{#1}}
    {\global\cslet{rugk@ref@backreflist@\detokenize{#1}}\@empty}
    {}%
  \rugk@ifprintableinlistcs{#2}{rugk@ref@backreflist@\detokenize{#1}}
    {}
    {\listcsgadd{rugk@ref@backreflist@\detokenize{#1}}{#2}}}

% write label backref to aux file
% {<label>}
\def\rugk@write@ref@backref#1{%
  \if@filesw
    \protected@write\@mainaux{}{\string\rugk@ref@backref
      {#1}{\thepage}{\noexpand\the\c@page}}%
  \fi}

\newcommand{\backref}[1]{%
  \rugk@write@ref@backref{#1}%
  \ref{#1}%
}
\newcommand{\autobackref}[1]{%
  \rugk@write@ref@backref{#1}%
  \autoref{#1}%
}

\newcounter{backrefpages}
\newcounter{totalbackrefpages}

\newcommand*{\printbackrefpage}[1]{%
  \stepcounter{backrefpages}%
  \hyperlink{page.#1}{#1}%
  \ifnumless{\value{backrefpages}}{\value{totalbackrefpages}}
    {, }
    {}}

\newrobustcmd*{\printlabelbackrefs}[1]{%
  \setcounter{backrefpages}{0}%
  \setcounter{totalbackrefpages}{0}%
  \ifcsundef{rugk@ref@backreflist@\detokenize{#1}}
    {Not referenced}
    {\def\do##1{\stepcounter{backrefpages}}%
     \dolistcsloop{rugk@ref@backreflist@\detokenize{#1}}%
     \setcounter{totalbackrefpages}{\value{backrefpages}}%
     \setcounter{backrefpages}{0}%
     \ifnumgreater{\value{totalbackrefpages}}{1}
       {pp.}
       {p.}~%
     \forlistcsloop{\printbackrefpage}{rugk@ref@backreflist@\detokenize{#1}}}}
\makeatother

\newrobustcmd{\backcaption}[3][]{%
  \if\relax\detokenize{#1}\relax
    \def\rugk@tmpcapt{\caption[#2]}%
  \else
    \def\rugk@tmpcapt{\caption[#1]}%
  \fi
  \rugk@tmpcapt{#2 (see \printlabelbackrefs{#3})}%
  \label{#3}}


\begin{document}
\section{Introduction}
\listoffigures

Here is a reference to \backref{test} and  \autobackref{test}

\clearpage

And here we talk about it again: \autobackref{test}

\clearpage 

\begin{figure}[p]
\backcaption{This is a test.}{test}
\end{figure}

\end{document}


Alternatif berdasarkan label dengan penghitung lari. Output backref hanya akan backref ke satu label backref per halaman dan label, tapi itu bisa diubah jika Anda suka (mungkin terlihat aneh karena Anda akan mendapatkan sesuatu seperti "pp. 1, 1").

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
\newcounter{backrefinst}

% stolen from biblatex
% we need a way to check if printable text is in a list
\newrobustcmd{\rugk@ifprintableinlist}[2]{%
  \begingroup
    \def\rugk@tempa{\endgroup
      \@secondoftwo}%
    \renewcommand*{\do}[1]{%
      \ifstrequal{##1}{#1}
        {\def\rugk@tempa{\endgroup
           \@firstoftwo}%
         \listbreak}
        {}}%
    \dolistloop{#2}%
  \expandafter\rugk@tempa}

\newrobustcmd{\rugk@ifprintableinlistcs}[2]{%
  \expandafter\rugk@ifprintableinlistcs@i\csname #2\endcsname{#1}}
\long\def\rugk@ifprintableinlistcs@i#1#2{\rugk@ifprintableinlist{#2}{#1}}

% add <page> to the list of pages where <label> was referenced
% remember the first instance counter for each label/page combination
% {<label>}{<backref instance counter>}{<page>}{<page int>}
\protected\def\rugk@ref@backref#1#2#3#4{%
  \ifcsundef{rugk@ref@backreflist@\detokenize{#1}}
    {\global\cslet{rugk@ref@backreflist@\detokenize{#1}}\@empty}
    {}%
  \rugk@ifprintableinlistcs{#3}{rugk@ref@backreflist@\detokenize{#1}}
    {}
    {\listcsgadd{rugk@ref@backreflist@\detokenize{#1}}{#3}%
     \csgdef{rugk@ref@backrefinst@\detokenize{#1}@\detokenize{#3}}{#2}}}

% write label backref to aux file
% {<label>}
\def\rugk@write@ref@backref#1{%
  \if@filesw
    \protected@write\@mainaux{}{\string\rugk@ref@backref
      {#1}{\the\value{backrefinst}}{\thepage}{\noexpand\the\c@page}}%
  \fi}

\newcommand*{\rugk@create@backref@label}[1]{%
  \begingroup
    \refstepcounter{backrefinst}%
    \label{backref.\the\value{backrefinst}}%
    \rugk@write@ref@backref{#1}%
  \endgroup
}

\newcommand{\backref}[1]{%
  \ref{#1}%
  \rugk@create@backref@label{#1}%
}
\newcommand{\autobackref}[1]{%
  \autoref{#1}%
  \rugk@create@backref@label{#1}%
}

\newcounter{backrefpages}
\newcounter{totalbackrefpages}

% {<label>}{<page>}
\newcommand*{\printbackrefpage}[2]{%
  \stepcounter{backrefpages}%
  \pageref{backref.\csuse{rugk@ref@backrefinst@\detokenize{#1}@\detokenize{#2}}}%
  \ifnumless{\value{backrefpages}}{\value{totalbackrefpages}}
    {, }
    {}}

\newrobustcmd*{\printlabelbackrefs}[1]{%
  \setcounter{backrefpages}{0}%
  \setcounter{totalbackrefpages}{0}%
  \ifcsundef{rugk@ref@backreflist@\detokenize{#1}}
    {Not referenced}
    {\def\do##1{\stepcounter{backrefpages}}%
     \dolistcsloop{rugk@ref@backreflist@\detokenize{#1}}%
     \setcounter{totalbackrefpages}{\value{backrefpages}}%
     \setcounter{backrefpages}{0}%
     \ifnumgreater{\value{totalbackrefpages}}{1}
       {pp.}
       {p.}~%
     \forlistcsloop{\printbackrefpage{#1}}{rugk@ref@backreflist@\detokenize{#1}}}}
\makeatother

\newrobustcmd{\backcaption}[3][]{%
  \if\relax\detokenize{#1}\relax
    \def\rugk@tmpcapt{\caption[#2]}%
  \else
    \def\rugk@tmpcapt{\caption[#1]}%
  \fi
  \rugk@tmpcapt{#2 (see \printlabelbackrefs{#3})}%
  \label{#3}}

\begin{document}
\section{Introduction}
\listoffigures

Here is a reference to \backref{test} and  \autobackref{test}

\clearpage

And here we talk about it again: \autobackref{test}

\clearpage 

\begin{figure}[p]
\backcaption{This is a test.}{test}
\end{figure}

\end{document}

Jika Anda ingin mencetak semua kejadian backref, kode ini dapat disederhanakan: Seseorang hanya perlu mengingat daftar jumlah contoh backref untuk setiap label dan bukan daftar halaman untuk setiap label.

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{etoolbox}
\usepackage{hyperref}

\makeatletter
\newcounter{backrefinst}

% add <page> to the list of pages where <label> was referenced
% remember the first instance counter for each label/page combination
% {<label>}{<backref instance counter>}
\protected\def\rugk@ref@backref#1#2{%
  \ifcsundef{rugk@ref@backreflist@\detokenize{#1}}
    {\global\cslet{rugk@ref@backreflist@\detokenize{#1}}\@empty}
    {}%
  \ifinlistcs{#2}{rugk@ref@backreflist@\detokenize{#1}}
    {}
    {\listcsgadd{rugk@ref@backreflist@\detokenize{#1}}{#2}}}

% write label backref to aux file
% {<label>}
\def\rugk@write@ref@backref#1{%
  \if@filesw
    \protected@write\@mainaux{}{\string\rugk@ref@backref
      {#1}{\the\value{backrefinst}}}%
  \fi}

\newcommand*{\rugk@create@backref@label}[1]{%
  \begingroup
    \refstepcounter{backrefinst}%
    \label{backref.\the\value{backrefinst}}%
    \rugk@write@ref@backref{#1}%
  \endgroup
}

\newcommand{\backref}[1]{%
  \ref{#1}%
  \rugk@create@backref@label{#1}%
}
\newcommand{\autobackref}[1]{%
  \autoref{#1}%
  \rugk@create@backref@label{#1}%
}

\newcounter{backrefpages}
\newcounter{totalbackrefpages}

% {<label>}{<page>}
\newcommand*{\printbackrefpage}[1]{%
  \stepcounter{backrefpages}%
  \pageref{backref.#1}%
  \ifnumless{\value{backrefpages}}{\value{totalbackrefpages}}
    {, }
    {}}

\newrobustcmd*{\printlabelbackrefs}[1]{%
  \setcounter{backrefpages}{0}%
  \setcounter{totalbackrefpages}{0}%
  \ifcsundef{rugk@ref@backreflist@\detokenize{#1}}
    {Not referenced}
    {\def\do##1{\stepcounter{backrefpages}}%
     \dolistcsloop{rugk@ref@backreflist@\detokenize{#1}}%
     \setcounter{totalbackrefpages}{\value{backrefpages}}%
     \setcounter{backrefpages}{0}%
     \ifnumgreater{\value{totalbackrefpages}}{1}
       {pp.}
       {p.}~%
     \forlistcsloop{\printbackrefpage}{rugk@ref@backreflist@\detokenize{#1}}}}
\makeatother

\newrobustcmd{\backcaption}[3][]{%
  \if\relax\detokenize{#1}\relax
    \def\rugk@tmpcapt{\caption[#2]}%
  \else
    \def\rugk@tmpcapt{\caption[#1]}%
  \fi
  \rugk@tmpcapt{#2 (see \printlabelbackrefs{#3})}%
  \label{#3}}

\begin{document}
\section{Introduction}
\listoffigures

Here is a reference to \backref{test} and  \autobackref{test}

\clearpage

And here we talk about it again: \autobackref{test}

\clearpage 

\begin{figure}[p]
\backcaption{This is a test.}{test}
\end{figure}

\end{document}
2 JohnKormylo Aug 24 2020 at 21:04

Solusi ini menggunakan \backcaptiondan \backrefsebagai pengganti \caption\labeldan \ref.

\documentclass{article}
\usepackage{hyperref}

\newcommand{\backref}[1]% #1 = label name
{\raisebox{\baselineskip}[\ht\strutbox]{\hypertarget{#1.back}{}}\ref{#1}}

\newcommand{\backcaption}[2]% #1 = caption, #2= label
{\caption[#1]{\hyperlink{#2.back}{#1}}\label{#2}}

\begin{document}
\listoffigures

\vspace{2in}
Here is a reference to \backref{test}.

\begin{figure}[p]
\backcaption{This is a test.}{test}
\end{figure}

\end{document}
1 rugk Aug 27 2020 at 00:36

Oke, terima kasih atas jawaban @John Kormylo saya mendapat solusi parsial, yaitu jika kita menggunakan yang biasa \labeltentu saja dapat digunakan \pagerefuntuk merujuk kembali ke halaman tersebut.

Satu-satunya masalah adalah: Ini juga hanya berfungsi jika kita hanya memiliki satu referensi .

Berikut beberapa kode:

\documentclass{article}
\usepackage[utf8]{inputenc}

\title{test-backref}
\date{August 2020}

\usepackage{hyperref}

% thanks https://tex.stackexchange.com/a/559834/98645
\newcommand{\backref}[1]% #1 = label name
{
    \raisebox{\baselineskip}[\ht\strutbox]{\label{#1.back}{}}%
    \ref{#1}
}
\newcommand{\autobackref}[1]% #1 = label name
{
    \raisebox{\baselineskip}[\ht\strutbox]{\label{#1.back}{}}%
    \autoref{#1}
}

\newcommand{\backcaption}[2]% #1 = caption, #2= label
{\caption[#1]{#1 (see p.~\pageref{#2.back})}\label{#2}}
\newcommand{\backcaptionoptional}[3]% #1 optional label, 2 = caption, #3= label
{\caption[#1]{#2 (see p.~\pageref{#3.back})}\label{#3}}


\begin{document}

\maketitle

\section{Introduction}
\listoffigures

\vspace{2in}
Here is a reference to \backref{test} and  \autobackref{test}….

\newpage

And here we talk about it again: \autobackref{test}

\newpage 

\begin{figure}[p]
\backcaption{This is a test.}{test}
\end{figure}

\end{document}

Perhatikan bahwa ini juga menunjukkan masalahnya. LaTeX (berhak) mengeluh:

Anda telah menggunakan label yang sama lebih dari sekali. Pastikan setiap \ label {...} hanya memberi label satu item.

Ini terjadi, karena saya telah menyebutkan angka itu beberapa kali. Pada akhirnya, keluarannya hanya menggunakan label terakhir sebagai acuan.

Jadi belum selesai, tapi saya kira kita perlu menggunakan penghitung atau lebih untuk menghitung label untuk solusi lengkap? Bukan? (Jika Anda memiliki ide, silakan kirimkan dan gunakan ini sebagai basis Anda.)