Cross-referencing with sentences? Is there any way for the LaTex to copy a certain segment from another .tex file?

Create a file textref.sty with the following contents:

\newcommand\save[2]{%
  \@bsphack\protected@write\@auxout{}{\string\newtext{#1}{#2}}\@esphack
  \newtext{#1}{#2}%
}
\newcommand\newtext[2]{\global\@namedef{text:#1}{#2}}
\newcommand\recall[1]{%
  \ifcsname text:#1\endcsname
    \@nameuse{text:#1}%
  \else
    \textbf{??? Unknown Text ???}%
  \fi
}   
\newcommand\saveAndRecall[2]{\save{#1}{#2}\recall{#1}}
\DeclareOption*{\PassOptionsToPackage{\CurrentOption}{xr}}
\ProcessOptions\relax
\RequirePackage{xr}
\RequirePackage{xpatch}
\xpatchcmd\XR@test{\fi\fi\fi}{\else\ifx#1\newtext\let\XR@tempa\@firstoftwo\fi\fi\fi\fi}{}{}

textref.sty defines three commands:

\save{label}{text} ... makes text accessible via label

\recall{label} ... replaces label by the corresponding saved text

\saveAndRecall{label}{text} ... combines the upper two commands, i.e., saves text with label and prints text

Moreover, the package loads the xr package; any options passed to textref are forwarded to the xr package such that its full functionality is available as well.

In the preamble of your documents, load the package with

\usepackage{textref}

As with \label and \ref, you have to run (pdf/xe/lua)latex twice to propagate the information.

This works also across file boundaries. You just have to include the foreign .aux file in the preamble using

\externaldocument{A}

Here is an example:

% A.tex
\documentclass{article}
\usepackage{textref}
\begin{document}
\section*{A.tex}
William Shakespeare once wrote: ``\saveAndRecall{hamlet}{Two beer or not two beer!}''.
Or did he?
\end{document}

% B.tex
\documentclass{article}
\usepackage{textref}
\externaldocument{A}
\begin{document}
\section*{B.tex}
\recall{hamlet}
\end{document}

First typeset A.tex, then B.tex.

enter image description here

enter image description here


The way closest to what you asked for I can think of would be to concatenate the bold text to a single macro - which will only help you if you want to include A.tex and B.tex in the same project.

Note that this will not create a file B.tex in which all the bold text is written down, but you can use the macro \myBoldText at any point of the document in order to display the content.

% ## In the header:
% Create an empty macro:
\def\myBoldText{}

% Define how text shall be separated in B.tex
\def\mySeparator{\newline}

% Create a command which appends input text to the macro
\newcommand{\addToMyBoldText}[1]{#1% <- you can modify here to always display bold
\expandafter\def\expandafter\myBoldText\expandafter{\myBoldText\mySeparator #1}}
% ## In A.tex
... \addToMyBoldText{The Kalman filter ... in control theory} ...
... \addToMyBoldText{In most applications ... are measured} ...
% ## In B.tex
\myBoldText

The package zref might be your friend:

By means of zref you can implement your own \label-\ref-like cross-referencing-mechanisms, e.g., for retrieving and storing sentences. By means of zref's module xr you can retrieve zref-cross-referencing-labels from other .tex-documents as long as the .aux-files of these documents are available.

I suggest using zref-route because with zref you

  • get warnings on the terminal/in the .log-file about multiply-defined labels if present.
  • are informed via terminal/.log-file about the need to re-run LaTeX in case data related to a label/related to something saved has changed.

The following file A.tex uses zref for providing macros \StoreSentence and \RetrieveSentence for making a sentence accessible via a zref-cross-referencing-label.

The following file B.tex uses the same things and additionally loads zref's xr-module for making available the zref-cross-referencing-data of A.tex/A.aux during compilation of B.tex.

A.tex:

\documentclass{article}

\usepackage{zref}

\makeatletter
\@ifpackageloaded{hyperref}{%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence[1]{%
      \texorpdfstring{\zref@refused{#1}}{}%
      \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
    }%
  }%
}{%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \nfss@text{\reset@font\bfseries ??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence[1]{%
      \zref@refused{#1}%
      \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
    }%
  }%
}%
%
\zref@newprop{storedsentence}[\RetrieveSentence@RefUndefText]{}%
\@ifdefinable\StoreSentence{%
  \DeclareRobustCommand\StoreSentence[2]{%
    \begingroup
    \zref@setcurrent{storedsentence}{#2}%
    \zref@labelbyprops{#1}{storedsentence}%
    \endgroup
    #2%
  }%
}%
\makeatother

\begin{document}

Let's retrieve the sentence associated to the label FirstSentence in A.tex:

\RetrieveSentence{FirstSentence}

\bigskip

Let's retrieve the sentence associated to the label SecondSentence in A.tex:

\RetrieveSentence{SecondSentence}

\bigskip\noindent\hrule\bigskip

Let's create the labels:

\vfill

This is some text which does not go to a label.

%\textbf{%
  \StoreSentence{FirstSentence}{This is some text which in A.tex went to the label FirstSentence.}%
%}

\vfill

This is some text which does not go to a label.

%\textbf{%
  \StoreSentence{SecondSentence}{This is some text which in A.tex went to the label SecondSentence.}%
%}

\vfill

\end{document}

Repeating compiling A.tex without deleting .aux-files between compilations until messages like

Package rerunfilecheck Warning: File `A.out' has changed.
(rerunfilecheck)                Rerun to get outlines right
(rerunfilecheck)                or use package `bookmark'.

and

LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

are not displayed on the terminal any more yields A.pdf:

enter image description here

B.tex:

\documentclass{article}

% This time let's load zref's module xr for importing labels from the .aux-files of other documents:
\usepackage[xr]{zref}
% Let's import the zref-cross-referencing-labels from A.tex/A.aux:
%\zexternaldocument[<prefix to add to names of imported labels>]{<external document>}[<url>]
\zexternaldocument[FromA:]{A}

\makeatletter
\@ifpackageloaded{hyperref}{%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence[1]{%
      \texorpdfstring{\zref@refused{#1}}{}%
      \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
    }%
  }%
}{%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \nfss@text{\reset@font\bfseries ??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence[1]{%
      \zref@refused{#1}%
      \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
    }%
  }%
}%
%
%\zref@newprop{storedsentence}[\RetrieveSentence@RefUndefText]{}%
%\@ifdefinable\StoreSentence{%
%  \DeclareRobustCommand\StoreSentence[2]{%
%    \begingroup
%    \zref@setcurrent{storedsentence}{#2}%
%    \zref@labelbyprops{#1}{storedsentence}%
%    \endgroup
%    #2%
%  }%
%}%
\makeatother

\begin{document}

Let's retrieve the sentence associated to the label FirstSentence in A.tex:

\RetrieveSentence{FromA:FirstSentence}

\bigskip

Let's retrieve the sentence associated to the label SecondSentence in A.tex:

\RetrieveSentence{FromA:SecondSentence}

\end{document}

As long as A.tex and A.aux from repetitive compiling of A.tex are available, repeating compiling B.tex without deleting .aux-files between compilations until messages like

Package rerunfilecheck Warning: File `B.out' has changed.
(rerunfilecheck)                Rerun to get outlines right
(rerunfilecheck)                or use package `bookmark'.

and

LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

are not displayed on the terminal any more yields B.pdf:

enter image description here


Caveats and possible pitfalls:

  • When you change A.tex, you need to compile A.tex repetitively to create A.aux and A.pdf which reflect the changes and then to compile B.tex repetitively to create B.pdf which reflects the changes in A.tex, too.

  • Commands/control sequences occurring in the text that is to be cross-referable across documents won't be expanded when writing the corresponding data to .aux-file:

    If commands/control-sequences (e.g., denoting mathematical symbols) are used in A.tex in places that are to be copied, then it must be ensured that these commands/control sequences get defined in B.tex, too. If, e.g., a mathematical symbol in A.tex comes from the amssymb-package, then B.tex needs to load that package, too.

    Numbered items of sectioning denoted by underlying commands/control sequences like \section{...} or \begin{equation} will be a problem, too, because the underlying control sequences, too, won't be expanded.

    In order to resolve this you may need to make every numbered thing cross-referable by placing a \label into A.tex. Then you can retrieve that data within B.tex via the package xr/xr-hyper.

    If you wish to also copy numbered items of sectioning, preserving the numbers, then probably the package docstrip is the better choice.

    I posted another answer exhibiting docstrip.


With the examples above in B.pdf you have textual phrases coming from A.tex/A.aux. But you don't have hyperlinks in B.pdf leading to the corresponding places in A.pdf.

If you need hyperlinks, you can load the hyperref-package and have \StoreSentence create a named destination/an anchor and store the name of that destination/anchor within the zref-label. Then \RetrieveSentence can retrieve that information for wrapping everything into a hyperlink. In the examples below the counter SentenceCounter is used for auto-generating unique names for the named destinations/anchors. In the examples below the macro \RetrieveSentence comes with a "starred variant" which does not create hyperlinks.

A.tex:

\documentclass{article}

\usepackage[unicode=true, pdfnewwindow]{hyperref}

% The code between \makeatletter..\makeatother can go into a package/a .sty-file
% of its own. If the hyperref-package shall  be used, then that .sty-file must
% be loaded _after_ the hyperref-package.
\makeatletter
\RequirePackage[xr]{zref}%
\zref@newlist{StoreAndRetrieveSentence}%
\zref@newprop{storedsentence}[\RetrieveSentence@RefUndefText]{}%
\zref@addprop{StoreAndRetrieveSentence}{storedsentence}%
\newcounter{SentenceCounter}%
\@ifpackageloaded{hyperref}{%
  \zref@newprop{NameOfDestination}[]{}%
  \zref@addprop{StoreAndRetrieveSentence}{NameOfDestination}%
  \@ifdefinable\@hashchar{\edef\@hashchar{\string#}}%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence{%
      \@ifstar\RetrieveSentenceAtStar\RetrieveSentenceAtNoStar
    }%
  }%
  \newcommand\RetrieveSentenceAtNoStar[1]{%
    \texorpdfstring{%
      \zref@refused{#1}%
      \zref@ifrefundefined{#1}{\@firstofone}{%
        \zref@ifrefcontainsprop{#1}{NameOfDestination}{%
          \zref@ifrefcontainsprop{#1}{externaldocument}{%
            \href[pdfnewwindow=true]{%
              \zref@extractdefault{#1}{externaldocument}{}%
              \@hashchar
              \zref@extractdefault{#1}{NameOfDestination}{}%
            }%
          }{%
            \hyperlink{\zref@extractdefault{#1}{NameOfDestination}{}}%
          }%
        }{\@firstofone}%
      }%
    }{\@firstofone}%
    {%
      \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
    }%
  }%
  \newcommand\RetrieveSentenceAtStar[1]{%
    \texorpdfstring{\zref@refused{#1}}{}%
    \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
  }%
  \@ifdefinable\StoreSentence{%
    \DeclareRobustCommand\StoreSentence[2]{%
      \texorpdfstring{%
        \stepcounter{SentenceCounter}%
        \hypertarget{SentenceDestination.\arabic{SentenceCounter}}{#2}%
        \begingroup
        \zref@setcurrent{storedsentence}{#2}%
        \zref@setcurrent{NameOfDestination}{SentenceDestination.\arabic{SentenceCounter}}%
        \zref@labelbylist{#1}{StoreAndRetrieveSentence}%
        \endgroup
      }{%
        \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
      }%
    }%
  }%
}{%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \nfss@text{\reset@font\bfseries ??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence{%
      \@ifstar\RetrieveSentenceAtStar\RetrieveSentenceAtNoStar
    }%
  }%
  \newcommand\RetrieveSentenceAtStar[1]{%
    \zref@refused{#1}%
    \zref@extractdefault{#1}{storedsentence}{\RetrieveNumber@RefUndefText}%
  }%
  \newcommand\RetrieveSentenceAtNoStar[1]{%
    \zref@refused{#1}%
    \zref@extractdefault{#1}{storedsentence}{\RetrieveNumber@RefUndefText}%
  }%
  \@ifdefinable\StoreSentence{%
    \DeclareRobustCommand\StoreSentence[2]{%
      \stepcounter{SentenceCounter}%
      #2%
      \begingroup
      \zref@setcurrent{storedsentence}{#2}%
      \zref@labelbylist{#1}{StoreAndRetrieveSentence}%
      \endgroup
    }%
  }%
}%
\makeatother

\begin{document}

Let's retrieve the sentence associated to the label FirstSentence in A.tex with hyperlink:

\RetrieveSentence{FirstSentence}

\bigskip

Let's retrieve the sentence associated to the label SecondSentence in A.tex with hyperlink:

\RetrieveSentence{SecondSentence}

\bigskip

Let's retrieve the sentence associated to the label FirstSentence in A.tex without hyperlink:

\RetrieveSentence*{FirstSentence}

\bigskip

Let's retrieve the sentence associated to the label SecondSentence in A.tex without hyperlink:

\RetrieveSentence*{SecondSentence}

\bigskip\noindent\hrule\bigskip

Let's create the labels:

\vfill

This is some text which does not go to a label.

%\textbf{%
  \StoreSentence{FirstSentence}{This is some text which in A.tex went to the label FirstSentence.}%
%}

\vfill

This is some text which does not go to a label.

%\textbf{%
  \StoreSentence{SecondSentence}{This is some text which in A.tex went to the label SecondSentence.}%
%}

\vfill

\end{document}

A.pdf:

enter image description here

B.tex:

\documentclass{article}

\usepackage[unicode=true, pdfnewwindow]{hyperref}

% The code between \makeatletter..\makeatother can go into a package/a .sty-file
% of its own. If the hyperref-package shall  be used, then that .sty-file must
% be loaded _after_ the hyperref-package.
\makeatletter
\RequirePackage[xr]{zref}%
\zref@newlist{StoreAndRetrieveSentence}%
\zref@newprop{storedsentence}[\RetrieveSentence@RefUndefText]{}%
\zref@addprop{StoreAndRetrieveSentence}{storedsentence}%
\newcounter{SentenceCounter}%
\@ifpackageloaded{hyperref}{%
  \zref@newprop{NameOfDestination}[]{}%
  \zref@addprop{StoreAndRetrieveSentence}{NameOfDestination}%
  \@ifdefinable\@hashchar{\edef\@hashchar{\string#}}%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \texorpdfstring{\nfss@text{\reset@font\bfseries ??}}{??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence{%
      \@ifstar\RetrieveSentenceAtStar\RetrieveSentenceAtNoStar
    }%
  }%
  \newcommand\RetrieveSentenceAtNoStar[1]{%
    \texorpdfstring{%
      \zref@refused{#1}%
      \zref@ifrefundefined{#1}{\@firstofone}{%
        \zref@ifrefcontainsprop{#1}{NameOfDestination}{%
          \zref@ifrefcontainsprop{#1}{externaldocument}{%
            \href[pdfnewwindow=true]{%
              \zref@extractdefault{#1}{externaldocument}{}%
              \@hashchar
              \zref@extractdefault{#1}{NameOfDestination}{}%
            }%
          }{%
            \hyperlink{\zref@extractdefault{#1}{NameOfDestination}{}}%
          }%
        }{\@firstofone}%
      }%
    }{\@firstofone}%
    {%
      \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
    }%
  }%
  \newcommand\RetrieveSentenceAtStar[1]{%
    \texorpdfstring{\zref@refused{#1}}{}%
    \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
  }%
  \@ifdefinable\StoreSentence{%
    \DeclareRobustCommand\StoreSentence[2]{%
      \texorpdfstring{%
        \stepcounter{SentenceCounter}%
        \hypertarget{SentenceDestination.\arabic{SentenceCounter}}{#2}%
        \begingroup
        \zref@setcurrent{storedsentence}{#2}%
        \zref@setcurrent{NameOfDestination}{SentenceDestination.\arabic{SentenceCounter}}%
        \zref@labelbylist{#1}{StoreAndRetrieveSentence}%
        \endgroup
      }{%
        \zref@extractdefault{#1}{storedsentence}{\RetrieveSentence@RefUndefText}%
      }%
    }%
  }%
}{%
  \@ifdefinable\RetrieveSentence@RefUndefText{%
    \DeclareRobustCommand\RetrieveSentence@RefUndefText{%
      \nfss@text{\reset@font\bfseries ??}%
    }%
  }%
  \@ifdefinable\RetrieveSentence{%
    \DeclareRobustCommand\RetrieveSentence{%
      \@ifstar\RetrieveSentenceAtStar\RetrieveSentenceAtNoStar
    }%
  }%
  \newcommand\RetrieveSentenceAtStar[1]{%
    \zref@refused{#1}%
    \zref@extractdefault{#1}{storedsentence}{\RetrieveNumber@RefUndefText}%
  }%
  \newcommand\RetrieveSentenceAtNoStar[1]{%
    \zref@refused{#1}%
    \zref@extractdefault{#1}{storedsentence}{\RetrieveNumber@RefUndefText}%
  }%
  \@ifdefinable\StoreSentence{%
    \DeclareRobustCommand\StoreSentence[2]{%
      \stepcounter{SentenceCounter}%
      #2%
      \begingroup
      \zref@setcurrent{storedsentence}{#2}%
      \zref@labelbylist{#1}{StoreAndRetrieveSentence}%
      \endgroup
    }%
  }%
}%
\makeatother

% Let's import the zref-cross-referencing-labels from A.tex/A.aux:
%\zexternaldocument[<prefix to add to names of imported labels>]{<external document>}[<url>]
\zexternaldocument[FromA:]{A}[./A.pdf] %<- hyperlinks must go to A.pdf !!!

\begin{document}

Let's retrieve the sentence associated to the label FirstSentence in A.tex with hyperlink:

\RetrieveSentence{FromA:FirstSentence}

\bigskip

Let's retrieve the sentence associated to the label SecondSentence in A.tex with hyperlink:

\RetrieveSentence{FromA:SecondSentence}

\bigskip

Let's retrieve the sentence associated to the label FirstSentence in A.tex without hyperlink:

\RetrieveSentence*{FromA:FirstSentence}

\bigskip

Let's retrieve the sentence associated to the label SecondSentence in A.tex without hyperlink:

\RetrieveSentence*{FromA:SecondSentence}

\end{document}

B.pdf:

enter image description here


Caveats and possible pitfalls:

  • When you change A.tex, you need to compile A.tex repetitively to create A.aux and A.pdf which reflect the changes and then to compile B.tex repetitively to create B.pdf which reflects the changes in A.tex, too.

  • Commands/control sequences occurring in the text that is to be cross-referable across documents won't be expanded when writing the corresponding data to .aux-file:

    If commands/control-sequences (e.g., denoting mathematical symbols) are used in A.tex in places that are to be copied, then it must be ensured that these commands/control sequences get defined in B.tex, too. If, e.g., a mathematical symbol in A.tex comes from the amssymb-package, then B.tex needs to load that package, too.

    Numbered items of sectioning denoted by underlying commands/control sequences like \section{...} or \begin{equation} will be a problem, too, because the underlying control sequences, too, won't be expanded.

    In order to resolve this you may need to make every numbered thing cross-referable by placing a \label into A.tex. Then you can retrieve that data within B.tex via the package xr/xr-hyper.

    If you wish to also copy numbered items of sectioning, preserving the numbers, then probably the package docstrip is the better choice.

    I posted another answer exhibiting docstrip.