1

Are there placeholders in LaTeX of the form:

A horse is \placeholder able to fly.

\placeholdertext{not}

which should produce an output like:

A horse is not able to fly.

So I look for an analogon of \footnotemark and \footnotetext that works with arbitrary content.

I would like to use that for example in combination with marginpar, when I want to explain some simple term in a paragraph with a complex tikz picture on the side:

Some long paragraph in which I mention some term\marginpar{\placeholder} that I want to explain on the side.

\placeholdertext{
    \begin{tikzpicture}
    % Picture for a better understanding of the used term.
    \end{tikzpicture}
}

Another paragraph with some term\marginpar{\placeholder} that I want to explain.

\placeholdertext{
    Some informative text...
}
9
  • 1
    I don't understand what you want. \newcommand{\placeholder}{not} is fine or not? Jan 19, 2016 at 13:18
  • Well, that's fine for one occurrence. But what if I would like to use this kind of placeholder repeatedly? Like in the example with \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. Jan 19, 2016 at 13:21
  • @user1742364 your question is not at all clear, the obvious answer as Romain says is that TeX is a macro expansion language (which is another way of saying placeholder) and so \newcommand\placeholder{...} would appear to be the answer. perhaps you could expand the example in the question to show your requirement. (I do not see any connection to footnote currently) Jan 19, 2016 at 13:39
  • 1
    why do you need the \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. Jan 19, 2016 at 13:48
  • @DavidCarliste, you are right that I do not really need it. It is only a matter of good looking and better structured LaTeX-code. It's the same thing with footnotes, I could define them at the place of their marker with \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. Jan 19, 2016 at 13:55

4 Answers 4

2

You can use the todonotes package to insert not in the margin:

\documentclass{article}
\usepackage{todonotes}

\begin{document}
A horse is \todo{not} able to fly.
\end{document}
2

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.


  1. 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.
  2. Which means that all of this code is untested and likely has typos and/or bugs.
  3. 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.
  4. 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.
1

Of course there are 'placeholders' in (La)TeX -- 'every' macro is a place holder that will be expanded to it's meaning (or is a primitive that does something different)

The xspace package is just for the correct spacing after the macro, but some will frown on this.

The question is now: Is it worth to use much of those place holders? Rather not.

In my opinion, it is better use a glossary, e.g. glossaries package.

\documentclass[a4paper]{scrartcl}

\usepackage{xspace}
\newcommand{\placeholder}{not\xspace}
\begin{document}

A horse is \placeholder able to fly.

\end{document}
4
  • Oh no a \xspace ;-) Jan 19, 2016 at 13:18
  • @RomainPicot: I see a conflict with.... you know whom I mean :D
    – user31729
    Jan 19, 2016 at 13:20
  • I added a full example to the original question to clarify what I want to achieve (hopefully :-) ) Jan 19, 2016 at 13:29
  • @user1742364: That's a fragment and no full example
    – user31729
    Jan 19, 2016 at 13:30
1

There's also the skeldoc package to insert some blank placeholder space. For example the following code:

\documentclass{article}
\usepackage{blindtext}
\usepackage{skeldoc}
\begin{document}
\section{My section}
\skelline
\Blindtext[1]
\skelpar[4]
\Blindtext[1]
\section{\skelline}
\Blindtext[1]
\end{document}

would produce the following document: enter image description here

More info and examples in the docs: https://github.com/mlhetland/skeldoc.sty

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .