How do I resolve this error - Missing $ inserted? - r

I get the error message:
! Missing $ inserted.
<inserted text>
$
l.4 .... Graphic was created using BioRender.com.}
\label{fig:label}
and am going absolutely bonkers trying to resolve the issue. The text in question is in a figure caption, and I have checked all my math expressions preceding the 'hinted' line. All my math expressions are correctly closed with $'s; I've removed all underscores (_); and I even removed a link to a website.
Is there a way for me to at least be directed to where the problem is located in my file?
I'm using Rmarkdown, papaja::apa6_pdf.
Kind regards to anyone who can help.
EDIT
Thanks for the responses.
I've managed to narrow down the problem. Here is a sample of the chunk I'm having issues with:
{r fig-label, include = TRUE, fig.cap="**This is the title of the image.** (**A**) Explanation A with a special character alpha $\\alpha$5.(**B**) Explanation B with a supercript degree symbol 60^o^C."}
if (Sys.info()[1]=="Windows"){
knitr::include_graphics("C:\\Path\\To\\My\\Image\\File.png")
} else if (Sys.info()[1]=="Mac OS"){
knitr::include_graphics("Path To My Image/File.png")
}
The problem arises with my supercripted degree symbol. Windows can effortlessly render the image to the correct size with all the text options (boldface, special characters, etc). My Mac fails to do so, and even removing the degree symbol makes my Mac include the ** around the text I want to have as boldface. Windows can make the image the correct size, Mac prints the image way too big.
I've followed all the debugging options explained at https://yihui.org/tinytex/r/#debugging.
I very much want (need) this to work on my Mac, so can anyone give me some advice on how I can get my mac to properly knit my markdown document?
Again, kind regards to anyone/everyone who is able to help or offer advice. Let me know if you need additional information.

Related

Line magic function `%%pycodestyle` not found

I am using pycodestyle_magic as a linter in Jupyter. I am following the instructions at
https://github.com/mattijn/pycodestyle_magic
But I get error with 1-cell checking be it '%%pycodestyle' or '%%flake8'.
1st ERROR
# 1st CELL
%load_ext pycodestyle_magic
# 2nd CELL
%%pycodestyle
a=1
print(a)
2nd ERROR
# 1st CELL
%load_ext pycodestyle_magic
# 2nd CELL
%%flake8
a=1
print(a)
1st ERROR
I'm basing this on your image you posted for '1st error' and not the inaccurate code you posted:
Note that the error in your first error was saying line magic and not cell magic because it was not looking for cell magic anymore. You can see it will look for cell magic on the first line of a cell by putting in %%fake_magic as a first line and then 2 + 2 as a second line of cell. Running that you'll see UsageError: Cell magic %%fake_magic not found.
Hence, your first 'error' is that you are missing that the first line of the cell where you want to use cell magic becomes special. You cannot have something else on the first line where you want to use cell magic, even if it is commented out. In the image, you have #%%flake8 above %%pycodestyle. If you remove that line, it should work.
2nd ERROR
I'm basing this on your image you posted for '2nd ERROR' and not the inaccurate code you posted:
You've stumbled upon a bug currently involving flake8. (Maybe same or more bugs seem to prevent the %%flake8 magic from working at all for now, see comments.)
The solution/workaround is very similar to the '1st ERROR'. Remove the line you were trying to comment out because it being there is causing an issue.
The reasoning appears to be complex about the way the %%flake8 cell magic appears to work behind the scenes so that you cannot have comments in the code content at this time or it won't work. (And the second time it sees that comment symbol, it throws the error you see.) This bug has been reported here.
But the solution/work-around for now is straightforward. Remove the complexity you added, and see if it works.
In case of the %%flake8 magic, running the demo notebook as shown there doesn't work as shown, and so there is indeed a bug in the current version that was introduced by actually a bug in flake8 that the extension uses, see the specific comment here and the link to the underlying issue over at the flake8 repo. (In fact the extent that it doesn't work may be greater than the notebook reflects because as discussed in the comments, even cells without commented lines fail to report any formatting issues. The demo actually doesn't have any without and so I got distracted noting it only didn't show the buffer error for the first one in the demo notebook but since it had a commented line I thought it was still related.)The other way to workaround it at present is to install an older version of flake8 as pointed out here. The solution was merged though and should be available soon, and so just avoiding the triggering code in simple cases like yours is probably easier.
Note about the inaccurate code:
Post code for what gave you the issue, not what you think gave you the issue. Part of why you are asked to provide code as text the way you ran it, it is so those looking into it can run it the same way. Plus they don't have to type and can easily run it as you did. Beyond that, there's more reason behind it: you can try to see if what you observed originally matches what you are seeing now.
If you had run what you posted under the heading '1st ERROR' as the code block text, you would have seen it without the error you showed in your image. Usually you'd then discern the error/difference yourself. In other words, starball's comment was trying to point you to why you should be doing that, and sorted that 1st error out yourself. That would ultimately make your point about the fragile nature of %%flake8 magic have more impact because it isn't diluted by report of an error that isn't really an error. Plus, you may have realized what was necessary to avoid '2nd ERROR' because that code block works without error no matter how many times you run it.
Technical notes:
Anyone wishing to try out pycodestyle_magic with some of this code and the demo notebooks can click here to launch such a session served via the mybinder.org. service with most things necessary already set up. Once the session starts up in the classic Jupyter notebook interface, you need to do some further preparation to use pycodestyle_magic. As detailed here, you can install it with pip install flake8 pycodestyle_magic. So open a new notebook with the Python kernel backing it using New drop down on the upper right side and then choose the Python3 kernel. When it opens, make a cell that has %pip install flake8 pycodestyle_magic and run it. Then put %load_ext pycodestyle_magic as a cell and run that. You can now try the magic with code or pull the demo notebooks over and run those.

Text line contains an invalid character

I am currently writing my bachelor thesis using Rmarkdown, and whenever i make changes I knit to PDF to check how it looks etc.
It worked perfectly until I wanted to check some small changes and after that I can´t get it to knit, this is the error message I get:
output file: bbb.knit.md
! Text line contains an invalid character.
l.28 pdftitle={^^K
space{3.5in} Portfolio},
Error: Failed to compile bbb.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See bbb.log for more info.
Bare in mind that I am learning R while writing my thesis so I do have a very low level of knowledge. Does anyone have any suggestions for where the mistake might be? Line 28 has no text by the way.
I have read some suggestions about deleting the auxiliary file but I dont even know what that is or where to find it, or if it could cause me any problems deleting it.
Hope someone has some tips as wasting a lot of time on this is the last I need right now! Thanks
The solution was to change the title input in YAML from "\vspace{3.5in} Portfolio"
to only "Portfolio".

Problems with unicode in R

I am having problems with printing unicode characters in R. Initially the problems started with me trying to plot some custom labels with ggplot, but I have found out that this problem runs deeper.
For example, the letter đ is represented with unicode code U+0100. This means that if I type
"\u0100"
in the console, I should get đ printed as output, right? However, this is not what happens. Instead, as the output I get:
<U+0100>
I don't understand why this is happening since my encoding is set to UTF-8. Does anyone have any ideas on how to solve this?
I tried using the following function:
stri_escape_unicode("<U+0100>")
but I just get the same output as before. Any help would be appreciated! I am using Macbook Pro from 2013.

Markdown syntax: problems with superscript

This is my first time using R so this is about as basic of a question as they come.
I am making a presentation using markdown (ioslides). I want to do something very simple which is to put something in superscript. According to the manual, the syntax is the following:
Lower bound = α^Full – K^ - α^Full^
The first time I tried running this, I got a pop-up screen with some options. Too quickly, I pressed enter and inadvertently chose something. Now the syntax won't run and I can't figure out what I did to undo it.
Can someone help? Thank you in advance.
Markdown allows HTML to be used if necessary, so you should be able to use the HTML sup tag.

invalid multibyte character crashes when script is loaded from source (umlauts / special characters)

EDIT:
Thx to suggestions from the mailing list I realized that the problem I got has nothing to do with Sweave or Latex. It´s some Mac OS X related issue. Whenever I run my script by selecting all and sending it to R it works.
When I use
source("myplainRcode.R")
i get the error message stated below
finally I got sweave working together with ggplot2 on my Mac OS X. I invoke Sweave inside R with
Sweave("myfile.Rnw")
which creates the desired latex output. Now that the basic tests work, I try to source my real world file and it crashes at the following line:
gl_bybranch = ddply(new_wans,.(period,Branchen),
function(X)data.frame(Geschäftslage=mean(X$sentiment)))
I guess it has either to do with the ".(period...)" or the "ä" . Unfortunately I can't change these labels because they are also used in legends. So, somewhere in my code these ugly umlauts will appear. Is there a way to escape them in Sweave? I can't believe that this is problem since Sweave is written by a German who probably have second most umlaut characters (behind Turkey).
The error message I get is: invalid multibyte character in Parser on line 195
Thx for any ideas in advance!
YAY ! i got it. Sorry for the noise everybody. I switched all three files (.Rnw, mysource.R , invokeSweave.R) to UTF-8 it finally worked. So everybody who works with Komodo on a Mac make sure to change your default encoding to UTF-8 !

Resources