Mathematica offers the Notation package to define custom notation, and it seems to work great until I save a notebook with custom notation as a package and try to use Needs["..."] to import the new notation. As documented on the Mathematica mailing list, this causes bizarre syntax errors when evaluating any call to Notation[]. The suggested solution is to manually open the notation notebook and evaluate it into the kernel, but this is a pain for files that I plan on distributing. If anyone has a better workaround for the problem, I'd really appreciate the help!
I think your problem lies in the way Mathematica treats Notation[], quote:
When defining your own notations it is
critically important to use the
notation templates. The reason for
this restriction is that the templates
pasted into a notebook contain
essential tag boxes embedded in the
correct way.
It may not be possible at all to have a package file call Notation[], but there is a suggestion of using ParsedBoxWrapper which I didn't have time to play around with right now.
There is a kludgy solution to your problem, which is that you put all the Notation[] assignments into one cell of the Notebook and make it an initialization cell (right click on the cell edge --> Initalization Cell). This will incidentally also create a .m file with just the contents of that cell (but this file will, alas, not work with Needs[] or <<). After that you can collapse the cell, make it uneditable, unevaluatable, and hey presto! Almost like what you want.
I should note that from a distribution point of view this kludge is rather nice since the .m file gets regenerated every time you load the main file so you don't need to ship multiple files. The only downside is a slightly ugly Notebook (i.e. one hidden cell), and a pop up asking whether you want to evaluate the initialization cells of the Notebook whenever you first evaluate something in that Notebook.
Related
When I use the following,
imshow(imread('image1.jpg'));
imshow(imread('image2.jpg'));
imshow(imread('image3.jpg'));
imshow(imread('image4.jpg'));
imshow(imread('image5.jpg'));
imshow(imread('image6.jpg'));
I got only image named image6.jpg in the output figure.
There is also an option figure,imshow(...); to view all the images each in new window.
But writing figure in each line where I need to view the image is a repeated and tedious process. Is there any other solution to get the same output as with figure,imshow(..);
without using figure function.
I mainly put on this question because while programming a lot somewhere we forget to use the figure function and so the image that we need to view wont be visible. It would have been overwritten by other image. So provide me some solution.
I ask this only for simplicity in writing the code. So if there is any solution, please mention.
Thanks in advance.
I'm not sure, but I don't think that there's a workaround to that. MATLAB basically changes the current figure handle to that of a new image when you use imshow. One thing you can do however is to make a copy of imshow in your local directory and edit it accordingly to make your own UDF.
What I would recommend however (so as to preserve functionality across systems) is that you open your code is an editor and replace all imshows with figure, imshow. This should be easy enough and it'll be easy to revert back as well.
I've encountered such a problem, and hope you guys could help me out here.
I have a plot in my GUI, contained multiple lines with different linspecs and a group of legends.
And I've made a context menu which should allow users to open the EXACTLY same plot(retaining all line settings, title, legends, and so forth) in a new window(default figure, where it is able to save/edit the figure).
However I couldn't find a simple way to migrate the plot, except re-run the plot commands which is quite complicated(plot different data, etc.)
So, I am looking for the solution in the following two ways:
is there a simple way to migrate the plot into new figure window?
or is it possible to save the plot directly with current interface?
For 2, I'd like to clarify that I only want to save the plot, not all GUI interface. I've tried saveas(handle.Plot,...) but it saved the GUI interface as an entity.
I hope the point has been made clear, thanks for your time. Cheers.
For this task you can probably use the builtin Matlab function copyobj which does exactly this (i.e. the first option mentionned in your question).
The following piece of code demonstrates its usage:
h1=figure;
a1=plot((1:100),rand(1,100),'r-');
hold on
plot((1:100),rand(1,100),'b+');
legend({'plot1';'plot2'});
h2=figure;
copyobj(get(h1,'children'),h2);
Hope it works as well in your case.
UPDATE: as far as I understand this, your second solution would involve the saveas function which unfortunately works with the figure environment and not with axes (as you experienced it). So a workaround would probably involve copying the desired axes to a new figure with the method given above and then use saveas.
I have a Mathematica Notebook with Plot's, Images, Tables and text that I would like to present in a file. I've tried to append >>> all the stuff in a nb or m put that leads to nothing (the plot comes up as -graphic-).
I'm sure a lot of people have done this before! What is the best approach for such a task?
Why don't you just save your current notebook as PDF, using the File menu, perhaps after applying a suitable stylesheet? or is this Mac only functionality?
As we all know, when one is to send email with some Mathematica code in it, the practice is to do the following
Select the cell, Cell->ConvertTo -> INPUT FORM, then COPY AS TEXT
Then paste the code into the email, or any other exchange media it is.
The only problem with the above, is that when, on the other end, one copies the text from email and paste it back into a Mathematica notebook, the code will then no longer appear as it was in the original form (before the converting to INPUT FORM).
Because when one inserts the text copied from the email to Mathematica notebook, it becomes a standard form cell. All well, except the original code formatting is all lost.
Try it if you have not, convert a cell to INPUT FORM and the code will change shape, and I could not find a way to get back the original form before this operation.
Is there a way to email Mathematica code, in text of course, so that it keeps the same shape, and appear the same as it was originally when it was in standard form before the conversion?
Thanks
--Nasser
Cells formatted with the Code style (ALT/CMD-8) retain all formatting when using copy-and-paste. I use them all the time to format postings for SO. Well, truth be told, I use Code cells pretty much all the time, except for one-shot or throw-away input.
There are other advantages to using the Code style. It makes it easy to evaluate all of the definitions in a notebook, without also evaluating all of the incidental and test expressions: ALT/CMD-click on the cell bracket of any of the Code cells and then press SHIFT-ENTER. Also, Code cells automatically become initialization cells, which is frequently exactly what you want for the definitions in a notebook. Finally, when Mathematica spots Code cells in a notebook it offers to create an auto-save package that contains only those cells.
The downside to Code cells is that you lose the automatic formatting that Mathematica performs on cells with the Input style -- but then again that automatic formatting is precisely what we are trying to avoid in the context of this question.
Copying the cell as a cell expression doest the trick. Just right-click the cell bracket and the Copy as > Cell expression.
The resulting code is unusable for posting in newsgroups, as it is highly unreadable, but copying to and from email and back into Mathematica seems to preserve layout in the few tests that I did.
Since the problem is sending the code over email, why not just send (a part of) the notebook as an attachment. No copy-pasting mess, and guaranteed identical formatting.
I use Mma mainly to solve relatively small problems.
I want to start using it also to prepare my presentations and documents, but I am having troubles to learn how to do it from the embedded help, and I guess some good resources may be available elsewhere.
Do you know any useful pointers (books, papers, videos ...)?
Do you have a "bag of tricks" to post here?
Edit
This question received two answers so far (#mzabsky's and Mr.Wizard's) and although both are useful, perhaps my concerns are much more basic. So I am posting an example of the kind of things I am unable to do (or understand how to discern how others did them).
I took the following example from The Mathematica Journal (the notebook at the left on the following image - click on the image to see full size):
So, some issues, just to get the idea of my troubles:
1) I copied the text to my .nb on the right, formatted it with the same style (text), but the appearance is different, so I guess the style definition is different. How can I copy the style definitions from one .nb to the other?
2) The table below the text block doesn't have an attached style. How was it formatted? Where is the background color defined?
I would like pointers to read (or videos to look, or whatever) about these issues. I don't want you to write down here a book on Mathematica formatting!
Summary of the links posted in answers
A Mathgroup thread (John Browne) and here (David Park and
Selwyn Hollis)
Advice from Bob Ueland
The Writing Assistant Palette
David Park's notes
Simon's documents
Tips for Mathematica SlideShow presenters
Notebook formatting
Presentations with Mathematica
Videos
Tips for Mathematica Slide Show Presenters
How to - Automatic Slide Show
Create a Lecture Notebook
I use Mathematica to take lecture notes in real time without any major issues (while the proud TeX guys struggle hard to keep up :) ). I have also used it for most math-related homework/assignments I wrote during past two and half years on university.
Before you start, you may want to look at some of these video tutorials.
Also, a few recommendations from me:
Keyboard shortcuts are the key to type fast. Ctrl+9 for inline math cell, Ctrl+6 for superscript, etc.
Learn symbol identifiers for the "esc - symbol - esc" notation. "sum" for Sum, "es" for empty set...you can find list of these in the Mathematica documentation. I have encountered only very few symbols I wanted to type that don't have the esc notation name (for example, leftwards double arrow or double right tee).
Type all math-related stuff into inline math cells. The math cell will do some of the math related formatting for you - put spaces where they belong, render all variables and symbols in italics, etc.
Use the preformatted templates found in "New"->"Styled Notebook".
Do not use ENTER for breaklines, individual paragraph should go into separate text cells (Ctrl+Shift+D) so Mathematica can break the content into individual pages/slides correctly.
In-built Mathematica PDF export sucks big-time; I use CutePDF printer for this.
Also, save often and back up often (Dropbox/Syncplicity are the ideal solution), one misplaced keyboard shortcut can turn hundred hours of work worth document into a goulash (trust me, been there) :)
Example of lecture notes I took in real-time during lecture (it is in Czech, but that doesn't matter much).
I agree with all that mzabsky said in his answer.
Here's a few of extra things:
I find it useful to make statements using a Text or DisplayFormula cell then manually group a Mathematica check/proof to the statement which is then collapsed and can be displayed when you want.
The Writing Assistant Palette has quite a few useful constructions in it that you can learn from.
Finally, I found it really useful to make my own style sheet for a couple reasons:
1) the built-in ones are a bit ugly; 2) it really helps you to understand how the notebooks work.
To see examples of the stylesheet I made (which I don't claim to be perfect - I didn't bother making it work in all screen environments) look at some of the files in ftp://ftp.physics.uwa.edu.au/pub/MATH2200/2010/. I use a similar stylesheet in all of my notes - I have many research projects primarily contained in Mma notebooks, eg http://arxiv.org/abs/1102.3043.
Like Mr Wizard, I also recommend David Park's notes as a starting point. Also, you should study stylesheets that you like by going to the Format menu and clicking "Edit Stylesheet". Don't forget to follow the links through the cascade of stylesheets (version 6 onwards).
To answer the questions in your edit: Once you are viewing a notebook's stylesheet, you can save it, edit it, and use it in your own documents. Stylesheets in
$UserBaseDirectory/SystemFiles/FrontEnd/StyleSheets are automatically available in the menu. You can then use that stylesheet in any notebook by simply selecting it from the menu.
The formating in the screenshot that you posted is all contained in the stylesheet. This includes the grey background in the table.
Addendum:
When distributing notebooks to others, if the stylesheet is external from the notebook, then other people will not see it as you do. To include the stylesheet into the current notebook, you need code like
ss = StyleDefinitions /. Options[EvaluationNotebook[]]
fn = ToFileName[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "StyleSheets"}, ss]
If[FileExistsQ[fn],
style=Get[fn];SetOptions[EvaluationNotebook[],StyleDefinitions->style];,
Print["Can not find file"]]
(Assuming the file is in stored in the conventional place)
Here's an EmbedStylesheet.m that is an improved version of the above.
A Mathgroup thread on embedded stylesheets, particularly here (John Browne) and here (David Park and Selwyn Hollis) might be of interest.
I find stylesheets problematical. In particular I don't like having to remember to embed a stylesheet before I give the notebook to someone else, or before I try to print from an unfamiliar computer. However, along with David Park's tutorial (referenced above by Mr Wizard), I find this advice from Bob Ueland very useful.
If you browse the back-issues of The Mathematica Journal you will see that articles are available in Notebook format. These may be a useful reference.
Edit
I cannot recall a good formatting tutorial at the moment. You can use Show Expression (Win: Shift+Ctrl+E) to view the code expression for a block, such as the table in your example. While one may not enter code by hand in the same form, it can give indications of the options or methods that are used. Code can also be procedurally produced as needed.
I will add links as I find or recall them.
David Park's StyleSheet creation notes
Tips for Mathematica SlideShow presenters
These are brief, but may still be useful:
Notebook formatting
Presentations with Mathematica