# Use LaTeX approach to create LyX document. (LaTeX coding and importing into LyX)

Specific Question: I have never use LyX but would like to know if there is a possibility to work on LaTeX using my favorite editor (like emacs or any text editor) and then add my code to LyX (including bibliography etc) but without any special formatting commands, and have a working LyX document that will seem and be editable just like if I used LyX to create it.

More General: What Should I do and what about I have to be careful while creating a LyX document by using the LaTeX approach (above). I mean that I can imagine these:

1. I could just copy my code with simple math and text and then press a key and import it as code in LyX environment and wait the appearance of the document to be as expected.

2. May me for equations would need to open a new box and add my equation's content separately from the other text

3. Do the same as above about the tables (with the simpler LaTeX commands I could imagine and then try to format it)

4. Import a .bib file into LyX and somehow in the step 1 (if I have in the text code \cite{key1} or \citep{key1} I am expecting normal LaTeX behavior of LyX.)

PS: I can imagine creating an empty document using LyX as a first step with just a sample of code to create some basic elements, and then export to LaTeX in order to see how I should do the above steps. (For example if I should use  or ... but as I have never use LyX I don't really know the cdorrect approach to get a typical LyX document.)

Edit: A somehow simple document that I would like to test if can imported in LyX without problems, is the next:

\documentclass{article}
\usepackage{multirow}
\usepackage{array}
\usepackage{natbib}
\begin{document}
This is a test file that we will try to import inside LyX' document and we could or should expect the word LyX'' to be inside regular \LaTeX{} single and then double quotation marks. Also contains a variable $x_t$ with $t$ being the superscript.

\begin{table}
\begin{tabular}{|l|m{3cm}|c|p{4cm}|}
\hline
\textbf{No} & \textbf{Empty column}&\textbf{Centered}& \textbf{Auto-Broken''}\\\hline
1 & &First row& This is a text that will span in two rows of $4\;cm$ maximum width\\\hline
2--3&\multicolumn{2}{c|}{\multirow{2}{*}{columns 3--3 centered}} & Text here not spaned\\\cline{4-4}
&\multicolumn{1}{c}{}&& Text here not spaned\\\hline
4& & Centered $F(x)$& Last spanned cell here with length of $4\;cm$\\\hline
\end{tabular}
\end{table}

According to citation~\cite{citation1} \citeauthor{citation2}, should have write that book~\citep{citation2}

\bibliographystyle{apalike}
\bibliography{MyBib}

\end{document}


With bibliography file MyBib.bib:

@article{citation1,
author={Konstantinos Leledakis},
year={2019},
journal={Never publishing\ldots}
}

@book{citation2,
author={Albert Einstein},
title={Never told this sh$\|$t!},
year={1920},
publisher={Should be published!}
}


The output should be close to the picture:

And the LyX environment should behave as it was created by LyX itself. For example, you should be able to edit an element of the tabular or change the subscript that I named "superscript" by mistake in the "variable" x_t.

PS: Added multirow just in case... but if it creates problems could be removed.

• Please don't ask why to do something like this. Just imagine that I know some basic things on using LaTeX via plain text editors but someone asks me for LyX code of a somehow simple LaTeX document and I want to deliver o LyX document like as if I had used LyX buttons to add every element... But such an approach (using buttons) for a real LaTeX user seems really complicated and time hungry to actually be done like this. – koleygr Mar 15 '19 at 9:00
• Hi, I deleted my previous answer. Lyx does offer an import option via file / import / LaTeX. I am not familiar with the quality of the import, but well worth a try. – JohnShape Mar 15 '19 at 10:34
• Thanks @JohanFit ... Before post my question I searched a little bit about it and thus your previous answer did not seem accurate to me and didn't upvoted. But I haven't use it and I don't want to install it (because it will possibly curry its own LaTeX version and I don't want to risk having more installed LaTeX versions on my system if it will not work.). Thanks anyway. – koleygr Mar 15 '19 at 10:40
• I have updated images and am skiping bibliography tests since you seem to be happy there are internal ways to edit, though I would be happier to use notepad when needed :-) – user170109 Mar 16 '19 at 20:29

This is not a full answer since you have so many different questions :-)

Thus I may have to add to it over time

You are correct to beware installing LyX on your working TeX system since on windows it hijacks & modifies the registry significantly, plus has many variations on other components e.g. GhostScript, dvipng, Perl etc.
In addition for features such as synctex it has many differences to the way a simple editor works.

However for a standard piece of tex using \parbox :-)

The TeX import will instantly produce

which is held in a plain text file of 93 lines !! and if we ignore the first 75 lines as similar to a dedicated preamble of settings the last few lines are

\end_header

\begin_body

\begin_layout Standard
\begin_inset Formula
$q_{1}^{*}=\frac{\text{number of women who had their second birth in 1971}}{\text{\parbox{4.5cm}{\centering number of women who had their first birth in 1970}}-\text{\parbox{4cm}{\centering number of women had their second birth in 1970}}}$

\end_inset

\end_layout

\end_body
\end_document


So in theory for a very basic standalone entry it may be easy to add a pre-set header with a small 4 line trailer

The only way to be sure for a more complex entry e.g. with hyperref (which has its own settings) would be to suggest a user tries import, then slowly address each interface error situation.

Well running the MWE through TeXworks just to confidence check my system is working then the usual first pass with pdfLaTeX throws the need to compile with a bibtex setting and then the PDF is as per example above (no need to show again) NOTE two steps were required.

Starting LyX without a template and simply importing the tex file generates this view and we may note a minor potential difference in the handling of spanned boxes

However it is only ONE click on View to get a confirmatory PDF

It is very easy with a few clicks on table property icons (just like in word/excel) to correct the tabular output

The proof of any "Round Tripping" is to compare the output and LyX has multiple choices of outputs to select as shown here.

and having selected PdfLaTeX as one suitable choice for comparison, we get this new TeX file.

%% LyX 2.3.0 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass{article}
\usepackage[latin9]{inputenc}
\usepackage{array}
\usepackage[authoryear]{natbib}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
%% Because html converters don't know tabularnewline
\providecommand{\tabularnewline}{\\}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\usepackage{multirow}
\usepackage{array}

\makeatother

\begin{document}
This is a test file that we will try to import inside LyX' document
and we could or should expect the word LyX'' to be inside regular
\LaTeX{} single and then double quotation marks. Also contains a variable
$x^{t}$ with $t$ being the superscript.

\begin{table}
\begin{tabular}{|l|m{3cm}|c|p{4cm}|}
\hline
\textbf{No}  & \textbf{Empty column} & \textbf{Centered} & \textbf{Auto-Broken''}\tabularnewline
\hline
1  &  & First row & This is a text that will span in two rows of $4\;cm$ maximum width\tabularnewline
\hline
2--3 & \multicolumn{2}{c|}{\multirow{2}{*}{columns 3--3 centered}} & Text here not spaned\tabularnewline
\cline{1-1} \cline{4-4}
& \multicolumn{1}{c}{} &  & Text here not spaned\tabularnewline
\hline
4 &  & Centered $F(x)$ & Last spanned cell here with length of $4\;cm$\tabularnewline
\hline
\end{tabular}
\end{table}

According to citation~\citet{citation1} \citeauthor{citation2},
should have write that book~\citep{citation2}

\bibliographystyle{apalike}
\bibliography{MyBib}

\end{document}


Without testing all the plain latex options I do not know if it is the best but the corrections work well in TeXworks producing a very credible output after the standard 2 pass run. As far as I can tell the rendered tabular positions should be neigh on identical to the corrected pdf above.

• Really thanks! But I would really more thankful if you could just create 1) a small real latex file with just the simpler math you can imagine and some text 2) a real latex table with just multicolumn and multirow simple entries (including multirow package in your preamble) and 3) a real latex file with bibliography (and its bib) of just two or three entries and the \bibliographystyle plus the \printbibliography command. Then try to import these 3 files (4 with the bibliography) one by one to separate LyX documents... and just add what works and what fails inside the link document. – koleygr Mar 16 '19 at 16:16
• If you think you need these files from me as MWE I can provide them... (Didn't thought about this approach while posting the question)... Also in the above procedure I would like you to test if the imported table for example is editable as it would be if you had created inside LyX... Thanks (+1) – koleygr Mar 16 '19 at 16:18
• As you may (or may not know) my skill in native TeX on a scale of 0-100 is -1, However I do know how to cut and paste an MWE until it compiles. So if you point me at an example you wish to test I can try it – user170109 Mar 16 '19 at 16:21
• Just wait for me to provide these files for you on my question... It will take 2-3 minutes – koleygr Mar 16 '19 at 16:22
• Edited with the additional test files (one is bibliography)... Whenever you want please try if it can be imported and edited as it was created by LyX. – koleygr Mar 16 '19 at 16:59