0

I am still learning tex. The following example allows me to check if a variable exists, and if it does then prints some text in the PDF. This works great. However, when I add in a href, this appears whether the variable exists or not. Driving me nuts as I cant find anything online even close to what im trying to do. Is it even possible?

\ifdefempty{\na}{}{\Sexpr{cve_descr_na}}\ is displaying a variable I have in R. This works well. However, \ifdefempty{\ds}{}{\Sexpr{cve_descr_ds}{\href{\Sexpr{url_ds_1}}{here }}}\ displays "here" even if ds is null.

\documentclass[11pt,a4paper, titlepage]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{tikz,fancyvrb}
\usepackage{helvet}%font
\usepackage{color}
\usepackage[hidelinks]{hyperref}
\usepackage{etoolbox}
\def\ds{cve_descr_ds}
\def\ui{cve_descr_ui}
\renewcommand{\familydefault}{\sfdefault}%sets fonts across document
\title{}
\date{}
\begin{document}
\fcolorbox{white}{green!65}{%
\parbox{17cm}{
\textbf{Your Cyber Vunerabilities}\\
\small
\ifdefempty{\na}{}{\Sexpr{cve_descr_na}}\\
\ifdefempty{\spf}{}{\Sexpr{cve_descr_spf}}\\
\ifdefempty{\ds}{}{\Sexpr{cve_descr_ds}{\href{\Sexpr{url_ds_1}}{here }}}\\
\ifdefempty{\ui}{}{\Sexpr{cve_descr_ui}{\href{\Sexpr{url_ui_1}}{here,}}}\\
}
}
\end{document}

Here is the resultant sweave; \documentclass[11pt,a4paper, titlepage]{article} \usepackage[margin=0.5in]{geometry} \usepackage{tikz,Sweave,fancyvrb} \usepackage{helvet}%font \usepackage{color} \usepackage[hidelinks]{hyperref} \usepackage{etoolbox} \def\ds{cve_descr_ds} \def\ui{cve_descr_ui}

\renewcommand{\familydefault}{\sfdefault}%sets fonts across document
\title{}
\date{}
\begin{document}
\input{HC-concordance}
\fcolorbox{white}{blue!65}{%
\parbox{5cm}{
\begin{center}
  \textbf{About You}\\
  \small
  The Lakes
  \\
  Your business ranks in the highest 25 percent
\end{center}
}
}
\fcolorbox{white}{green!65}{%
\parbox{13cm}{
\textbf{Your Cyber Vunerabilities}\\
\small
\ifdefempty{\ds}{}{they can find more details{\href{}{here, }}}\\
\ifdefempty{\ui}{}{they can find more details {\href{https://nvd.nist.gov/vuln/detail/CVE-2016-6329}{here, }}}

 }

\end{document}
Siobhan
  • 41
  • (1) welcome, (2) please make your example compilable. I'm not sure whether R or Sweave is relevant for this, so perhaps don't add them. Note that since Sweave is not on CTAN not every latex user have access to that package. – daleif Dec 08 '17 at 14:48
  • Besides you are missing the } for \fcolorbox. We need to know what \Sexpr is doing, and it might not be compatible with hyperref expansion. – daleif Dec 08 '17 at 14:53
  • Ah ok , thanks! I missed the } in a poor copy and paste! Ill update my post. – Siobhan Dec 08 '17 at 14:56
  • But … \ds isn't empty in your document! – Harald Hanche-Olsen Dec 08 '17 at 15:10
  • this is where im a complete newbie im afraid. \def\ds{cve_descr_ds} is picking up variable cve_descry_ds from R script. In the example im running, in one R script loop it is null and another it is populated. the tex code returns cve_descr_ds when it exists and does nothing if it doesn't. However, when it doesn't exist I still get the "here". – Siobhan Dec 08 '17 at 15:16
  • No, that definition just puts the name cve_descr_ds as the expansion of \ds. If you want the contents of a variable from R in there, more magic is needed. I can't tell you what magic, though, since I am unfamiliar with R. – Harald Hanche-Olsen Dec 08 '17 at 15:18
  • The \Sexpr{cve_descr_ui} brings back the value from R. – Siobhan Dec 08 '17 at 15:27
  • Okay. But then the next question becomes rather technical: Does that expand “in TeX's mouth” all the way to the desired answer? If so, \edef\ui{\Sexpr{cve_descr_ui}} will put the value in \ui. If not, and this is more likely, the resulting contents of \ui will be some bunch of cryptic TeX commands to get at the value. And that is not good enough for what you want to do. – Harald Hanche-Olsen Dec 08 '17 at 15:31
  • One more comment: Have you looked at the other questions tagged r? In particular, this one looks interesting. (But again, not knowing R, I can't judge.) By the way, your question should also be tagged r. I'll do it for you. – Harald Hanche-Olsen Dec 08 '17 at 15:32
  • It is probably better if you post the output from Sweave instead of the Rtex file (which the above presumably is) – daleif Dec 08 '17 at 15:41
  • Thanks, and yes it is RTex. Ill update with the sweave version. – Siobhan Dec 08 '17 at 15:44
  • Ive mostly sorted the problem by creating the string in R, ifelse(is.na(url_ui_1)|is.null(url_ui_1)|url_ui_1=="", ui_string1<-"", ui_string1<-paste0("\href{",url_ui_1,"}{ here}") )which only writes the string if the value is not null. However, now I need to pass \href in the string, but R gives me Error: '\h' is an unrecognized escape in character string starting ""\h" – Siobhan Dec 11 '17 at 10:53

0 Answers0