Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have the following construction for a figure. The background is, when \submit is defined, only the caption is included, but no figure. Otherwise the figure is included.

The problem is that I have two places in which the same caption needs to be put. It is a pain to keep this in sync. I'm hoping there is a easy way to save the text to a value, and input it to both places. I have multiple such figures, so I'd like to do this in as generic a way as possible. So, if I have n figures, I'd like one command or function, not n, to do the job. Also, this is for a journal which doesn't like extra packages, so I need a solution that doesn't require a special package. I'd also be interested in solutions that use a different approach.

\newcommand{\figlegend}{% figure legend
  \refstepcounter{figure}% Increment counter for correct referencing
}
\ifdefined\submit \figlegend
Caption text
\label{schema}
\else
\par \mbox{}
\par
\begin{figure}[tp]
  \begin{center}
    \tikzsetnextfilename{Figure1-schema}
    \input{SCHEMA}
    \caption{%
        Caption text
            }
    \label{schema}
  \end{center}
\end{figure}
\fi

EDIT: I've edited the title to reflect more accurately what I'm trying to do here. @Mike Renfro thought that one could solve the issue by not including the figure if \submit was active. So, I've changed the code to what I'm actually using. The \input{SCHEMA} inputs a SCHEMA.tex file which contains TikZ/PGF code.

EDIT2: Edited title again. Unusually, I've accepted an answer that did not answer my original question. @Mike Renfro made a compelling case that I was asking the wrong question. I think his is a better answer to the question I wasn't asking, which was how to abstract out my conditional construct. Thanks Mike!

For reference, I'm including the Guidelines for Figure and Table Preparation of the journal in question, in question, PLoS ONE.

share|improve this question

3 Answers 3

up vote 5 down vote accepted

This sounds like this question, at least for this specific application. In that case, something like:

\documentclass{article}
\usepackage[demo]{graphicx} % demo option only useful for demonstration purposes
%\newcommand{\submit}{1}
\ifdefined\submit
  \relax
\else
  \renewcommand{\includegraphics}[2][]{\fbox{}}
\fi
\begin{document}
Here's some text.
\begin{figure}[tp]
  \centering
  \includegraphics[width=0.5\textwidth]{foo}
  \caption{Some text}
  \label{schema}
\end{figure}

\end{document}

returns

enter image description here

if the submit definition is commented (i.e, a separate draft mode), and

enter image description here

if it's active (i.e., ready to submit). If the restriction on extra packages isn't absolute, you could also use the existing draft mode of the documentclass with the ifdraft package.

EDIT -- modified version with reversed logic (\submit means omit the figure) and no output except the caption when the figure is suppressed. Replace the \ifdefined...\fi block above with:

\ifdefined\submit
  \renewcommand{\includegraphics}[2][]{\relax}
\else
  \relax
\fi

SECOND EDIT -- to make this work without simply redefining \includegraphics away:

with submit defined:

enter image description here

without submit defined:

enter image description here

\documentclass{article}
\usepackage{tikz} \usetikzlibrary{external} \tikzexternalize
\newcommand{\figlegend}{% figure legend
  \refstepcounter{figure}% Increment counter for correct referencing
}
\newcommand{\submit}{1}
\ifdefined\submit
  \newcommand{\makefigure}[3]{\figlegend #2 \label{#1}}
\else
  \newcommand{\makefigure}[3]{%
    \par
    \mbox{}
    \par
    \begin{figure}[tp]
      \centering
      \tikzsetnextfilename{figure-#3} \input{#3}
      \caption{#2}
      \label{#1}
    \end{figure}
}
\fi
\begin{document}

Here's some text.

\makefigure{schema}{The Caption}{basic}

\end{document}

If for some reason you need extra control over the generated figure, add extra arguments to the \makefigure definition and insert # placeholders accordingly.

share|improve this answer
    
I'm sorry, but I don't follow your answer at all. Are you sure you are answering the question I asked? –  Faheem Mitha Sep 14 '11 at 16:20
    
I think so: "The background is, when \submit is defined, only the caption is included, but no figure. Otherwise the figure is included." I guess I reversed the logic, though. But this does include an actual figure when \submit is defined, and omits it when it isn't. –  Mike Renfro Sep 14 '11 at 21:05
    
That's the background, yes. But that is only background to the question and not the question. The code I pasted (with details omitted) already deals with "when \submit is defined, only the caption is included, but no figure. Otherwise the figure is included." I'm still not clear on what you are trying to answer. My question is how I can find a macro which will allow me to substitute the same block of text in multiple places, without typing it in multiple times. I am sorry if I was not clear. –  Faheem Mitha Sep 14 '11 at 21:30
    
My answer doesn't deal at all with saving text to be used in multiple places, because in the context of making figure graphics disappear under certain conditions, it works, and without having to save any text, because the caption (the text you wanted to save) only appears once in the code. It's a matter of my answering a different question because I wasn't sure if you were asking the right one, or if you were making things more complicated than necessary. –  Mike Renfro Sep 14 '11 at 22:22
    
I see. In one case you are replacing \includegraphics with an (essentially) do nothing command. That does sound like a reasonable approach. I think I may have tried this before, but ran into problems. I'm not using \includegraphics, you see, but TikZ/PGF. I'll edit the question to show what I'm actually doing. On the face of it, putting the conditional in a macro rather than all over the place multiple times certainly seems like a more reasonable approach. Sorry for being so slow. The reversed logic thing is the journal, not me. They want the submit version without figures. –  Faheem Mitha Sep 15 '11 at 1:01

(Based on @0x06d64's answer.) The most "LaTeX-like" way of doing things I guess would be to use a \newcommand to save the text (first line below), but that definition would be restricted to the environment in which \savestring appears. If you want to be able to use \savestring inside an environment and that #1 be still defined outside, you need to make a global assignment, for instance using \gdef. As this is a TeX primitive, \gdef{#1}{#2} would not work.

%\newcommand{\savestring}[2]{\newcommand{#1}{#2}}
\newcommand{\savestring}[2]{\gdef#1{#2}}

Used as \savestring\stringA{Some text to save} ... \stringA.

Actually, we could simply use \gdef\stringA{Some text to save} directly. Dunno what I was thinking about.

share|improve this answer
    
Thanks, Bruno. That looks good. One tangentially related question. When browsing for related solutions, I've regularly come across doubled hashes in nested macro definitions. Quote: "If macro is defined inside another macro definition, TeX needs doubled number of #." I notice your solution works without this. What am I missing? PS: \sringA should be \stringA at the end. –  Faheem Mitha Sep 15 '11 at 3:20
    
@Faheem: Here we want to use the argument given to \savestring in a \gdef. If for instance you wanted the command given to \savestring to take an argument, you would have to double the hash for the argument of the inside command: e.g., if you do \newcommand{\gdefonearg}[1]{\gdef#1##1{...##1...}} the \gdefonearg defines its argument #1 to be a command that take one argument, ##1 and surround it with dots ...##1.... –  Bruno Le Floch Sep 15 '11 at 16:26

You could use

\newcommand{\sometext}{Some text}

This allows also to pass additional parameters, see this wikibook. I'm not sure if this is the preferred method for simple string replacements.

share|improve this answer
    
Thanks. I'd like one command that will work for all cases. To be clear, I'd prefer if the actual string was not part of the definition of the macro. For one thing, there is a place for user defined macros at the beginning of the document, and I can't reasonably put text specific stuff there. Does that make sense? –  Faheem Mitha Sep 14 '11 at 8:11
    
Something like \newcommand{\savestring}[2]{\newcommand{#1]{#2}} where one sticks in the macro for the string as first arg, and the actual text as second arg, theoretically looks like a possible approach, but (unsurprisingly) is rejected by LaTeX. –  Faheem Mitha Sep 14 '11 at 8:24
    
@Faheem: two possible problems. (1) The last ] should be a }. (2) \newcommand defines commands locally. If you want a global definition, you will need \gdef#1{#2}. That should work. –  Bruno Le Floch Sep 14 '11 at 14:49
    
@Bruno: What last ] is that? What would the whole thing look like, please? Thanks. –  Faheem Mitha Sep 14 '11 at 15:36
    
@Bruno: Ok, you mean \newcommand{\savestring}[2]{\gdef{#1}{#2}}? –  Faheem Mitha Sep 14 '11 at 16:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.