3

I have a document where many of its values are defined in a separate file via \newcommand, and added to the main document using \input in the preamble. The file being included is the output of a program, and defines more than 50 new commands, most of them just numbers.

In order to identify which pieces of the text have been automatically generated, I changed the program to create variables with the text in a grey box, as in:

\newcommand{\StemLoopStart}{\colorbox[gray]{0.8}{35}}
\newcommand{\StemLoopEnd}{\colorbox[gray]{0.8}{50}}

In my exact case, I also had to set \fboxsep to a small number because of problems in line spacing that included these values, but mostly, this fixed my issue.

However, I'm now trying to perform calculations with those values using the fp package which obviously does not work.

So, is there any alternative method to do this, one that would still allow to me to make calculations? I could change the program one more time to output two commands per variable, one meant for text and another for the calculations, or even have the program make the calculations and create the variables itself.

EDIT:

Here's the document as an example:

\documentclass[10pt,a4paper,onecolumn,article]{memoir}

%% A lot of other extra packages being loadded and configuration

\usepackage{color}
\usepackage{fp}

%% each of following files is generated automatically and may have
%% more than 50 new commands. A typical file would be:
%%
%%  \newcommand{\StemLoopStart}{\colorbox[gray]{0.8}{35}}
%%  \newcommand{\StemLoopEnd}{\colorbox[gray]{0.8}{50}}
%%
%% repeated many times, with different variable names

\input{results/variables-align_results}
\input{results/variables-cluster_stats}
\input{results/variables-protein_stats}
\input{results/variables-utr}
\input{data/variables-Marzluff-stats}

\begin{document}

  %% in the vast majority of the cases I will want text to be displayed
  %% in gray so the following will work fine
  Chicken histone genes have a stem loop starting at \StemLoopStart{}
  blah blah blah.

  %% However, in very few cases, I want to make some calculations
  \FPeval{StemLoopLength}{clip(\StemLoopEnd{}-\StemLoopStart{})}
  The typical length of an histone stem loop is \StemmLoopLength{}
  which ...

\end{document}

The calculation won't work because StemLoopEnd is not just a number. And because there are a lot of variables and I am using most of them many times in the text, using colorbox each time, or redefining them all again manually in the preamble is not a viable solution.

On the other hand, calculations are only needed very few times but I don't think it is possible to undo the colorbox. Also, the program cannot be changed to issue non colorbox in some cases, either in all or none.

So, short of making the program issue a colorbox and non colorbox version of each variable, is there a way to do this?

4

Another option is to redefine the coloring when you are within an \FPeval. This way you have your commands defined as:

\newcommand{\StemLoopStart}{\ColorBox{35}}
\newcommand{\StemLoopEnd}{\ColorBox{50}}

where I defined:

\newcommand{\ColorBox}[1]{\colorbox[gray]{0.8}{#1}}.

Then before the \begin{document} you can rdefine \FPeval to disable this \ColorBox and restore the definition afterwards:

\let\OldFPeval\FPeval
\renewcommand\FPeval[2]{%
    \renewcommand{\ColorBox}[1]{##1}%
    \OldFPeval{#1}{#2}%
    \renewcommand{\ColorBox}[1]{\colorbox[gray]{0.8}{##1}}%
}

A further enhancement is to provide a \FPeval* which will then add the highlighting to the computed number:

\let\OldFPeval\FPeval
\makeatletter
\RenewDocumentCommand{\FPeval}{s m m}{%
    \renewcommand{\ColorBox}[1]{##1}%
    \IfBooleanTF{#1}{%
        \OldFPeval{#2@NoColor}{#3}%
        \expandafter\newcommand\csname#2\endcsname{\ColorBox{\csname#2@NoColor\endcsname}}%

    }{%
        \OldFPeval{#2}{#3}%
    }%
    \renewcommand{\ColorBox}[1]{\colorbox[gray]{0.8}{##1}}%
}
\makeatother

Then the MWE yields:

enter image description here

where \FPeval{StemLoopLength}{...} does not get highlighted, but \FPeval*{StemLoopLengthWithColor}{...} is highlighted.

Notes:

  • The filecontents package was only used to package this example into a fully compilable example. It is not needed in your real example.

Code:

\documentclass{article}

%% each of following files is generated automatically and may have
%% more than 50 new commands. A typical file would be:
%%
%%  \newcommand{\StemLoopStart}{\colorbox[gray]{0.8}{35}}
%%  \newcommand{\StemLoopEnd}{\colorbox[gray]{0.8}{50}}
%%
%% repeated many times, with different variable names
%%

%\usepackage{file contents}% Commented out to prevent overwrite of variables-align_results.tex
\begin{filecontents*}{variables-align_results.tex}
    \newcommand{\StemLoopStart}{\ColorBox{35}}
    \newcommand{\StemLoopEnd}{\ColorBox{50}}
\end{filecontents*}
\fboxsep=1pt

%% A lot of other extra packages being loaded and configuration

\usepackage{xcolor}
\usepackage{xparse}
\usepackage{fp}


\input{variables-align_results}
%\input{results/variables-align_results}
%\input{results/variables-cluster_stats}
%\input{results/variables-protein_stats}
%\input{results/variables-utr}
%\input{data/variables-Marzluff-stats}

\newcommand{\ColorBox}[1]{\colorbox[gray]{0.8}{#1}}%

\let\OldFPeval\FPeval
\makeatletter
\RenewDocumentCommand{\FPeval}{s m m}{%
    \renewcommand{\ColorBox}[1]{##1}%
    \IfBooleanTF{#1}{%
        \OldFPeval{#2@NoColor}{#3}%
        \expandafter\newcommand\csname#2\endcsname{\ColorBox{\csname#2@NoColor\endcsname}}%

    }{%
        \OldFPeval{#2}{#3}%
    }%
    \renewcommand{\ColorBox}[1]{\colorbox[gray]{0.8}{##1}}%
}
\makeatother

\begin{document}

  %% in the vast majority of the cases I will want text to be displayed
  %% in gray so the following will work fine
  Chicken histone genes have a stem loop starting at \StemLoopStart{}
  blah blah blah.

  %% However, in very few cases, I want to make some calculations
  \FPeval{StemLoopLength}{clip(\StemLoopEnd{}-\StemLoopStart{})}%
  The typical length of an histone stem loop is \StemLoopLength{}
  which makes the end at \StemLoopEnd.


  %% Using the \FPeval* will highlight that value in the output.
  \FPeval*{StemLoopLengthWithColor}{clip(\StemLoopEnd{}-\StemLoopStart{})}%
  The typical length of an histone stem loop is \StemLoopLengthWithColor{}
  which makes the end at \StemLoopEnd.
\end{document}
  • Turns out that this means that any use of the variables after the first use of FPeval will disable the colouring for all variables that follow. For example, if you end your document with "which ... makes the end at \StemLoopEnd{}." you will see that the value in \StemLoopEnd won't be coloured anymore. Do you know how to work around this? – carandraug Sep 2 '15 at 19:28
  • @carandraug: Updated solution to fix mentioned problem. – Peter Grill Sep 5 '15 at 17:19
  • Thank you. I would upvote you and mark it correct one more time if I could ;) Also, for anyone reading this in the future, if the value computed by FPeval should also be inside the gray box, the end of macro can instead be \OldFPeval{NoColourResult}{#2} \renewcommand{\ColorBox}{\OldColorBox} \expandafter\newcommand\csname #1\endcsname{\ScriptValue{\NoColourResult}} – carandraug Sep 5 '15 at 23:12
  • @carandraug: Updated solution which now defines an \FPeval* that applies the highlighting to computer values. – Peter Grill Sep 6 '15 at 6:16
4

Your question is not very clear to be honest (It always helps to have a complete document to test).

But it seems that you can make the program write

\newcommand{\StemLoopStart}{\foo{35}}

Then locally have

\newcommand\foo[1]{\colorbox[gray]{0.8}{#1}}

when you want colour and

Then locally have

\newcommand\foo[1]{#1}

when you want the bare numbers.

I can not guess your intention with the final comment

However, what I was really hoping for was something such as the following in the preamble:

\colorbox[gray]{0.8}{\input{program-output}}

\colorbox[gray]{0.8}{\input{program-output}} may or may not already be legal syntax, depending what is in the file to be input, or perhaps you'd want

\colorbox[gray]{0.8}{\parbox{\textwidth}{\input{program-output}}}

To put multi-line material into a box, but either way it would go in the document body (after \begin{document}) not in the preamble (before \begin{document}).

  • I am sorry, my question was not clear. I have expanded the question with an example of what I have (the actual files are far too long) and the problems. – carandraug Feb 7 '14 at 17:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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