From the discussion in the comments, it appears that what is wanted is the ability to define a placeholder after it's used.
First, a quick note: The original poster talked about using \footnotemark
in a paragraph and then \footnotetext
at the end of the paragraph for the sake of aesthetics in their source code. This is a bad idea. For one thing, there is no guarantee that the \footnotetext
and \footnotemark
will end up on the same page. For another, the plan breaks down if there is more than one footnote in a paragraph (in that case, the footnote
counter will need to be manually adjusted so that the footnote texts will have the correct number).
That said, because of the way LaTeX interprets its input, we can't ordinarily define something after it's used.¹ The way around this is to take advantage of the mechanism that LaTeX uses to handle other forward definitions, the .aux
file.
So we'll want to be able to do something along the lines of:
A horse is \PH{horse} able to fly.
\SetPlaceholder{horse}{not}
Since I'm doing this off the top of my head,² I'm going to omit niceties like warnings about undefined placeholders or redefined placeholders. I'll leave that as an exercise for the reader.
Defining \PH
is going to be relatively simple. It will just look for a command named PH@name
for some name
given and use that for output. I'm going to use some internal LaTeX commands³ here so we'll need to start with
\makeatletter
and then define \PH
as:
\NewDocumentCommand{\PH}{ m }
{\@nameuse{#1}}
A placeholder will be defined by:
\NewDocumentCommand{\DefinePlaceHolder}{ m m }
{\@namedef{#1}{#2}}
You might be thinking, wait a minute, weren't we going to define \SetPlaceHolder
? What's this \DefinePlaceHolder
?
This is the command that will get written to the .aux
file to create the placeholder. So we define \SetPlaceHolder
to be:
\NewDocumentCommand{\SetPlaceHolder}{ m m }
{\@bsphack % ❶
\protected@write\@auxout{}% ❷
{\string\DefinePlaceHolder % ❸
{#1}{#2}}%
\@esphack % ❶
}
A couple notes here: The sequence \@bsphack
…\@esphack
means that \SetPlaceHolder
won't cause extra spaces to be produced in the output if it appears in the middle of a paragraph.⁴ We use \protected@write
to write the output to the aux file ❷. This will expand any macros that appear that aren't protected with \protect
although we can get the same effect with what we're doing by using \string
❸ here.
There's still a lot of improvements that can be made to this code. I would recommend doing texdoc source2e
and jumping down to File F which documents how the cross reference code works. There are some challenges to be had if you want the possibility of an empty placeholder since otherwise foo \PH{bar} bam
would output two spaces between foo and bam if the placeholder is empty but again, I'll leave all of that as an exercise for the reader.
- This is largely because TeX is essentially a one-pass compilation system. Us older folks who've programmed in 1970s–80s programming languages will be familiar with having to do forward declarations if you want to use a variable or function before it's declared.
- Which means that all of this code is untested and likely has typos and/or bugs.
- I was contemplating using Expl3, but a lot of what we want requires other internal commands so I might as well use
\@nameuse
over \use:c
.
- This is used by many LaTeX commands that don't produce output and the code involved is rather interesting. Among other things it will make sure that you still get end-of-sentence spacing if you write, e.g.,
foo.\label{bar} And more…
The one gotcha is that if you have a command that uses the sphack
mechanism at the end of a paragraph it can produce a space there that would not otherwise be output which under certain rare circumstances can cause a blank line to appear at the end of a paragraph.
\newcommand{\placeholder}{not}
is fine or not?\footnotemark
and\footnotetext
, I would like to mark the place where I want to add additional contents and then specify these contents after the paragraph.\placeholder
macro at all it does not seem to be doing anything useful, why not just put the tikzpicture in the marginpar (if you want to define \placeholder you can of course do that but a macro that is only used once for each definition is never really needed.\footnote
but that would somehow destroy the paragraph - I prefer to use\footnotemark
and later give the contents with\footnotetext
. Of course I can define a command for each explaination, but then I would need to define dozens of commands that are used only once each, as you said, and the code for the tikzpicture/explaination/... would not be next to the paragraph.